在方法调用</string>中创建列表<string>

时间:2013-12-04 19:18:59

标签: c# list methods

我正在调用一个方法,并希望在其中发送一个字符串列表。是否可以在调用方法时创建列表?

这样的事情:

methodToCall(int myInt, {"Hello", "World"});

然后像这样检索它:

public static void methodToCall(int myInt, list<string> myStrings)
{
}

3 个答案:

答案 0 :(得分:6)

是的,看起来像这样:

呼叫

 methodToCall(2,new List<string>() {"Hello", "World"});

和这个函数一样调用

 public static void methodToCall(int inInt, List<string> inList)

答案 1 :(得分:2)

将您的方法签名修改为:

public static void methodToCall(int myInt, List<string> myStrings)
{ }

然后将其称为:

methodToCall(5, new List<string> { "Hello", "World" });

答案 2 :(得分:0)

当然,您可以创建如下数组:new [] {"Hello", "World"}

这将导致

methodToCall(myInt, new[] {"Hello", "World"});

并且另一方的签名可能看起来像

public static void MethodToCall(int m, IEnumerable<string> s){}