2010-11-16 19 views
3

当您将此列表作为方法的参数时,这意味着什么?此列表<string> ListName作为参数吗?

public static void KillZombies(this List<Zombie> ZombiesToKill, int NumberOfBullets) 
{ 
    ... 
} 
+0

找不到它,但我敢肯定这个问题是重复的... – 2010-11-16 21:10:02

回答

8

这将意味着,该方法是Extension Method

调用方法的代码可能看起来有点混乱:

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

在现实中,这是一种语法糖其中相当于:

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

调用代码如下所示:

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

它看起来像是当KillZombies被调用时,只有NumberOfBullets被传递。如果ZombiesToKill没有作为参数发送,通常定义在哪里?我对代码缺乏表示歉意,如果没有任何上下文,这是不可能回答的,我明白。 – sooprise 2010-11-16 20:32:49

+0

@Soo - 我只是更新了我的答案,希望能够解释为什么你看到你是什么。 – 2010-11-16 20:36:57

+0

非常好,非常感谢! – sooprise 2010-11-16 21:08:19

1

这是一个extension method

在这种情况下,延伸List<Zombie>。你会这样称呼它:

listOfZombies.KillZombies(numberOfBullets); 

listOfZombies类型是List<Zombie>numberOfBullets是一个整数。

+0

...你通过了我,但我正在回来! :-P – 2010-11-16 20:32:06

+0

@Justin Niessner - Hehehe ...比赛正在进行中:) – Oded 2010-11-16 20:33:01