Jam*_*xon 12 c# optional-parameters params
所以我看到有可能有一个方法签名,其中第一个参数提供默认值,第二个参数是params集合.
我看不到的是实际使用第一个参数的默认值的方法.
它可能吗?
示例方法:
void WaitAllTasks(string message = "Running Task.WaitAll", params Task[] tasks);
我最初尝试在调用方法时省略message参数,并尝试使用命名参数,这与params不兼容.
它编译,但是可以使用它吗?
Jon*_*eet 23
我可以找到三种方法来调用方法而不指定第一个参数的值:
using System;
class Test
{
static void PrintValues(string title = "Default",
params int[] values)
{
Console.WriteLine("{0}: {1}", title,
string.Join(", ", values));
}
static void Main()
{
// Explicitly specify the argument name and build the array
PrintValues(values: new int[] { 10, 20 });
// Explicitly specify the argument name and provide a single value
PrintValues(values: 10);
// No arguments: default the title, empty array
PrintValues();
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有找到一种方法来指定多个值而不显式构建数组...
| 归档时间: |
|
| 查看次数: |
3873 次 |
| 最近记录: |