可选参数之后PARAMS [英] Optional argument followed by Params
问题描述
因此,我认为,它可能有一个方法签名,其中第一个参数提供一个默认值,第二个参数是一个PARAMS集合。
So I see that it's possible to have a method signature where the first parameter provides a default value and the second parameter is a params collection.
我可以'看不到是实际使用的第一个参数的默认值的一种方式。
What I can't see is a way to actually use the default value of the first argument.
是它在所有可能的?
实例方法:
无效WaitAllTasks(字符串消息=运行Task.WaitAll,则params任务[]任务);
我最初试图调用该方法时省略消息参数和使用命名参数,它不使用参数工作也试过。
I initially tried omitting the message parameter when calling the method and also tried using named parameters, which doesn't work with params.
它编译,但有可能使用它?
It compiles, but is it possible to use it?
推荐答案
我能找到的三种方式调用该方法不指定第一个参数的值:
I can find three ways of calling the method without specifying a value for the first parameter:
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();
}
}
我还没有找到指定多个值的方法没有明确建立阵列虽然...
I haven't found a way of specifying multiple values without explicitly building the array though...
这篇关于可选参数之后PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!