Mr.*_*ith 6 c# arrays methods initialization
如果我有这样的方法:
public void DoSomething(int Count, string[] Lines)
{
//Do stuff here...
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样称呼它?
DoSomething(10, {"One", "Two", "Three"});
Run Code Online (Sandbox Code Playgroud)
什么是正确的(但希望不是很长的路)?
Sim*_*ver 18
你可以这样做 :
DoSomething(10, new[] {"One", "Two", "Three"});
Run Code Online (Sandbox Code Playgroud)
如果所有对象都是相同类型,则不需要在数组定义中指定类型
Mar*_*off 10
如果 DoSomething是可以修改的函数,则可以使用params关键字传递多个参数而不创建数组.它也将正确接受数组,因此不需要"解构"现有数组.
class x
{
public static void foo(params string[] ss)
{
foreach (string s in ss)
{
System.Console.WriteLine(s);
}
}
public static void Main()
{
foo("a", "b", "c");
string[] s = new string[] { "d", "e", "f" };
foo(s);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./d.exe a b c d e f
| 归档时间: |
|
| 查看次数: |
9357 次 |
| 最近记录: |