Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this List<string> ListName as a parameter?

Tags:

c#

list

this

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

public static void KillZombies(this List<Zombie> ZombiesToKill, int NumberOfBullets)
{
    ...
}
like image 644
sooprise Avatar asked Jan 26 '26 05:01

sooprise


1 Answers

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);
like image 143
Justin Niessner Avatar answered Jan 27 '26 17:01

Justin Niessner