【已更新最新开发文章,点击查看详细】

这两种技术都可与方法、索引器、构造函数和委托一起使用。

使用命名参数和可选参数时,将按实参出现在实参列表(而不是形参列表)中的顺序计算这些实参。

此功能极大地方便了对 COM 接口(例如 Microsoft Office 自动化 API)的调用。

命名实参

例如,通过以函数定义的顺序按位置发送实参,可以采用标准方式调用打印订单详细信息(例如卖家姓名、订单号和产品名称)的函数。

PrintOrderDetails("Gift Shop", 31, "Red Mug");

如果不记得形参的顺序,但却知道其名称,则可以按任意顺序发送实参。

PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");

PrintOrderDetails(productName: "Red Mug", sellerName: "Gift Shop", orderNum: 31);

由于 sellerName 和 productName 都是字符串类型,所以使用命名实参而不是按位置发送实参是有意义的,可以区分这两种类型并减少代码阅读者的困惑。

当命名实参与位置实参一起使用时,只要

  • 没有后接任何位置实参或

PrintOrderDetails("Gift Shop", 31, productName: "Red Mug");

  • 在以下示例中,形参 orderNum 位于正确的位置,但未显式命名。

PrintOrderDetails(sellerName: "Gift Shop", 31, productName: "Red Mug");

但是,如果其后接位置实参,则无序命名实参无效。

// 出现 CS1738 警告: 命名参数规范必须出现在指定了所有固定参数之后。
PrintOrderDetails(productName: "Red Mug", 31, "Gift Shop");

示例

以下代码执行本节以及某些其他节中的示例。

class NamedExample
{
    static void Main(string[] args)
    {
        // 使用位置参数以常规方式调用方法。
        PrintOrderDetails("Gift Shop", 31, "Red Mug");

        // 可以按任意顺序为参数提供命名参数。
        PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");
        PrintOrderDetails(productName: "Red Mug", sellerName: "Gift Shop", orderNum: 31);

        // 与位置参数混合的命名参数只要在正确的位置使用就有效
        PrintOrderDetails("Gift Shop", 31, productName: "Red Mug");
        PrintOrderDetails(sellerName: "Gift Shop", 31, productName: "Red Mug");    // C# 7.2 onwards
        PrintOrderDetails("Gift Shop", orderNum: 31, "Red Mug");                   // C# 7.2 onwards

        // 但是,如果使用顺序不对,则混合参数无效。
        // 下列声明会引起编译错误
        // PrintOrderDetails(productName: "Red Mug", 31, "Gift Shop");
        // PrintOrderDetails(31, sellerName: "Gift Shop", "Red Mug");
        // PrintOrderDetails(31, "Red Mug", sellerName: "Gift Shop");
    }

    static void PrintOrderDetails(string sellerName, int orderNum, string productName)
    {
        if (string.IsNullOrWhiteSpace(sellerName))
        {
            throw new ArgumentException(message: "Seller name cannot be null or empty.", paramName: nameof(sellerName));
        }

        Console.WriteLine($"Seller: {sellerName}, Order #: {orderNum}, Product: {productName}");
    }
}
可选实参

任何调用都必须为所有必需的形参提供实参,但可以为可选的形参省略实参。

默认值必须是以下类型的表达式之一:

  • 常量表达式;

  • struct;

  • default(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#4.0新增功能02 命名实参和可选实参

还可通过使用 .NET OptionalAttribute 类声明可选参数。 OptionalAttribute 形参不需要默认值。

Main 中的代码演示了可用于调用构造函数和方法的不同方式。
 1 namespace OptionalNamespace
 2 {
 3     class OptionalExample
 4     {
 5         static void Main(string[] args)
 6         {
 7             // 实例 anexample 不发送构造函数可选参数的参数。
 8             ExampleClass anExample = new ExampleClass();
 9             anExample.ExampleMethod(1, "One", 1);
10             anExample.ExampleMethod(2, "Two");
11             anExample.ExampleMethod(3);
12 
13             // 实例anoThereExample为构造函数的可选参数发送参数。
14             ExampleClass anotherExample = new ExampleClass("Provided name");
15             anotherExample.ExampleMethod(1, "One", 1);
16             anotherExample.ExampleMethod(2, "Two");
17             anotherExample.ExampleMethod(3);
18 
19             // 以下语句产生编译器错误。
20 
21             // 必须为第一个参数提供参数,并且该参数必须是整数。
22             //anExample.ExampleMethod("One", 1);
23             //anExample.ExampleMethod();
24 
25             // 您不能在提供的参数中留下空白。
26             //anExample.ExampleMethod(3, ,4);
27             //anExample.ExampleMethod(3, 4);
28 
29             // 可以使用命名参数使前一条语句工作。
30             anExample.ExampleMethod(3, optionalint: 4);
31         }
32     }
33 
34     class ExampleClass
35     {
36         private string _name;
37 
38         // 因为构造函数name的参数有一个指定给它的默认值,所以是可选的
39         public ExampleClass(string name = "Default name")
40         {
41             _name = name;
42         }
43 
44         // 第一个参数(必需)没有指定默认值。因此,它不是可选的。OptionalStr和OptionalInt都有分配给它们的默认值。它们是可选的。
45         public void ExampleMethod(int required, string optionalstr = "default string",
46             int optionalint = 10)
47         {
48             Console.WriteLine("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
49                 optionalint);
50         }
51     }
52 
53     // 输出:
54     // Default name: 1, One, and 1.
55     // Default name: 2, Two, and 10.
56     // Default name: 3, default string, and 10.
57     // Provided name: 1, One, and 1.
58     // Provided name: 2, Two, and 10.
59     // Provided name: 3, default string, and 10.
60     // Default name: 3, default string, and 4.
61 
62 }
COM 接口

命名实参和可选实参,以及对动态对象的支持和其他增强功能大大提高了与 COM API(例如 Office Automation API)的互操作性。

这些形参如下图所示:

C#4.0新增功能02 命名实参和可选实参

在 C# 3.0 以及早期版本中,每个形参都需要一个实参,如下例所示。

var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Workbooks.Add();
excelApp.Visible = true;

var myFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatAccounting1;
excelApp.get_Range("A1", "B4").AutoFormat(myFormat, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

在下面的调用中,仅为 7 个形参中的其中一个指定了值。

excelApp.Range["A1", "B4"].AutoFormat( Format: myFormat );

有关详细信息和示例,请参阅操作说明:在 Office 编程中使用命名参数和可选参数操作说明:使用 Visual C# 功能访问 Office 互操作对象

重载决策

使用命名实参和可选实参将在以下方面对重载决策产生影响:

  • 如果方法、索引器或构造函数的每个参数是可选的,或按名称或位置对应于调用语句中的单个自变量,且该自变量可转换为参数的类型,则方法、索引器或构造函数为执行的候选项。

  • 将忽略可选形参已省略的实参。

  • 这是重载决策中的常规引用的结果,该引用用于参数较少的候选项。

 

【已更新最新开发文章,点击查看详细】

相关文章:

  • 2021-10-05
  • 2021-08-31
  • 2021-11-23
  • 2021-07-28
  • 2021-09-12
猜你喜欢
  • 2021-07-06
  • 2021-10-10
  • 2021-05-30
  • 2021-10-14
  • 2021-09-08
  • 2022-01-10
  • 2022-01-11
相关资源
相似解决方案