开发者

this List<string> ListName as a parameter?

What does it mean when you have a this List as a parameter to a method?

public stat开发者_JS百科ic void KillZombies(this List<Zombie> ZombiesToKill, int NumberOfBullets)
{
    ...
}


That would mean that the method is an Extension Method:

The code calling the method might look a little confusing:

var zombies = new List<Zombie>();
zombies.KillZombies(15);

In reality, this is a kind of syntactic sugar which is equivalent to:

public static void KillZombies(List<Zombie> zombiesToKill,
                               int numberOfBullets)
{
    // Code here
}

With the calling code looking like:

var zombies = new List<Zombie>();
KillZombies(zombies, 15);


It is an extension method.

In this case, extending List<Zombie>. You would call it like this:

listOfZombies.KillZombies(numberOfBullets);

Where the type of listOfZombies is List<Zombie> and numberOfBullets is an integer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜