深入解析C#中的命名实参和可选实参

2019-12-26 17:32:55刘景俊

方法、构造函数、索引器或委托的定义可以指定其形参为必需还是可选。任何调用都必须为所有必需的形参提供实参,但可以为可选的形参省略实参。
每个可选形参都具有默认值作为其定义的一部分。如果没有为该形参发送实参,则使用默认值。默认值必须是一个表达式的以下类型之一:
常数表达式;
窗体 new ValType()的表达式, ValType 是值类型,例如 枚举 或 结构;
窗体 默认 (ValType)的表达式, ValType 是值类型。
可选形参在形参列表的末尾定义,位于任何必需的形参之后。如果调用方为一系列可选形参中的任意一个形参提供了实参,则它必须为前面的所有可选形参提供实参。实参列表中不支持使用逗号分隔的间隔。例如,在以下代码中,使用一个必选形参和两个可选形参定义实例方法 ExampleMethod。


public void ExampleMethod(int required, string optionalstr = "default string",
  int optionalint = 10)

下面对 ExampleMethod 的调用导致编译器错误,原因是为第三个形参而不是为第二个形参提供了实参。


anExample.ExampleMethod(3, ,4);

但是,如果您知道第三个形参的名称,则可以使用命名实参来完成任务。


anExample.ExampleMethod(3, optionalint: 4);

IntelliSense 使用括号指示可选形参,如下图所示。

深入解析C#中的命名实参和可选实参

 

示例
在下面的示例中,ExampleClass 的构造函数具有一个形参,该形参是可选的。实例方法 ExampleMethod 具有一个必需的形参:required,以及两个可选形参:optionalstr 和 optionalint。 Main 中的代码演示了可用于调用构造函数和方法的不同方式。


namespace OptionalNamespace
{
  class OptionalExample
  {
    static void Main(string[] args)
    {
      // Instance anExample does not send an argument for the constructor's
      // optional parameter.
      ExampleClass anExample = new ExampleClass();
      anExample.ExampleMethod(1, "One", 1);
      anExample.ExampleMethod(2, "Two");
      anExample.ExampleMethod(3);

      // Instance anotherExample sends an argument for the constructor's
      // optional parameter.
      ExampleClass anotherExample = new ExampleClass("Provided name");
      anotherExample.ExampleMethod(1, "One", 1);
      anotherExample.ExampleMethod(2, "Two");
      anotherExample.ExampleMethod(3);

      // The following statements produce compiler errors.

      // An argument must be supplied for the first parameter, and it
      // must be an integer.
      //anExample.ExampleMethod("One", 1);
      //anExample.ExampleMethod();

      // You cannot leave a gap in the provided arguments. 
      //anExample.ExampleMethod(3, ,4);
      //anExample.ExampleMethod(3, 4);

      // You can use a named parameter to make the previous 
      // statement work.
      anExample.ExampleMethod(3, optionalint: 4);
    }
  }

  class ExampleClass
  {
    private string _name;

    // Because the parameter for the constructor, name, has a default
    // value assigned to it, it is optional.
    public ExampleClass(string name = "Default name")
    {
      _name = name;
    }

    // The first parameter, required, has no default value assigned
    // to it. Therefore, it is not optional. Both optionalstr and 
    // optionalint have default values assigned to them. They are optional.
    public void ExampleMethod(int required, string optionalstr = "default string",
      int optionalint = 10)
    {
      Console.WriteLine("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
        optionalint);
    }
  }
}