NET 4.0可选参数的使用方法

Visual C# 2010 引入了命名实参和可选实参。 利用“命名实参”,您将能够为特定形参指定实参,方法是将实参与该形参的名称关联,而不是与形参在形参列表中的位置关联。 利用“可选实参”,您将能够为某些形参省略实参。 这两种技术都可与方法、索引器、构造函数和委托一起使用。

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

命名实参

有了命名实参,您将不再需要记住或查找形参在所调用方法的形参列表中的顺序。 可以按形参名称指定每个实参的形参。 例如,可以采用标准方式调用计算身体质量指数 (BMI) 的函数,方法是依照该函数定义的顺序按位置发送体重和身高的实参。
1 CalculateBMI(123, 64);

如果不记得形参的顺序,但却知道其名称,您可以按任意顺序(先发送体重或先发送身高)发送实参。

1 CalculateBMI(weight: 123, height: 64);
2 
3 CalculateBMI(height: 64, weight: 123);

命名实参还可以标识每个实参所表示的含义,从而改进代码的可读性。

命名实参可以放在位置实参后面,如此处所示。

1 CalculateBMI(123, height: 64);

但是,位置实参不能放在命名实参后面。 下面的语句会导致编译器错误。

1 //CalculateBMI(weight: 123, 64);

以下是代码示例:

 1 class NamedExample
 2 {
 3     static void Main(string[] args)
 4     {
 5         // The method can be called in the normal way, by using positional arguments.
 6         Console.WriteLine(CalculateBMI(123, 64));
 7 
 8         // Named arguments can be supplied for the parameters in either order.
 9         Console.WriteLine(CalculateBMI(weight: 123, height: 64));
10         Console.WriteLine(CalculateBMI(height: 64, weight: 123));
11 
12         // Positional arguments cannot follow named arguments.
13         // The following statement causes a compiler error.
14         //Console.WriteLine(CalculateBMI(weight: 123, 64));
15 
16         // Named arguments can follow positional arguments.
17         Console.WriteLine(CalculateBMI(123, height: 64));
18     }
19 
20     static int CalculateBMI(int weight, int height)
21     {
22         return (weight * 703) / (height * height);
23     }
24 }

可选参数

 

方法、构造函数、索引器或委托的定义可以指定其形参为必需还是可选。 任何调用都必须为所有必需的形参提供实参,但可以为可选的形参省略实参。

 

每个可选形参都具有默认值作为其定义的一部分。 如果没有为该形参发送实参,则使用默认值。 默认值必须是一个表达式的以下类型之一:

 

  • 常数表达式;

  • 窗体 new ValType()的表达式, ValType 是值类型,例如 枚举 或 结构;

  • 窗体 默认 (ValType)的表达式, ValType 是值类型。

 

可选形参在形参列表的末尾定义,位于任何必需的形参之后。 如果调用方为一系列可选形参中的任意一个形参提供了实参,则它必须为前面的所有可选形参提供实参。 实参列表中不支持使用逗号分隔的间隔。 例如,在以下代码中,使用一个必选形参和两个可选形参定义实例方法 ExampleMethod

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

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

1 //anExample.ExampleMethod(3, ,4);

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

1 anExample.ExampleMethod(3, optionalint: 4);

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

 1 namespace OptionalNamespace
 2 {
 3     class OptionalExample
 4     {
 5         static void Main(string[] args)
 6         {
 7             // Instance anExample does not send an argument for the constructor's
 8             // optional parameter.
 9             ExampleClass anExample = new ExampleClass();
10             anExample.ExampleMethod(1, "One", 1);
11             anExample.ExampleMethod(2, "Two");
12             anExample.ExampleMethod(3);
13 
14             // Instance anotherExample sends an argument for the constructor's
15             // optional parameter.
16             ExampleClass anotherExample = new ExampleClass("Provided name");
17             anotherExample.ExampleMethod(1, "One", 1);
18             anotherExample.ExampleMethod(2, "Two");
19             anotherExample.ExampleMethod(3);
20 
21             // The following statements produce compiler errors.
22 
23             // An argument must be supplied for the first parameter, and it
24             // must be an integer.
25             //anExample.ExampleMethod("One", 1);
26             //anExample.ExampleMethod();
27 
28             // You cannot leave a gap in the provided arguments. 
29             //anExample.ExampleMethod(3, ,4);
30             //anExample.ExampleMethod(3, 4);
31 
32             // You can use a named parameter to make the previous 
33             // statement work.
34             anExample.ExampleMethod(3, optionalint: 4);
35         }
36     }
37 
38     class ExampleClass
39     {
40         private string _name;
41 
42         // Because the parameter for the constructor, name, has a default
43         // value assigned to it, it is optional.
44         public ExampleClass(string name = "Default name")
45         {
46             _name = name;
47         }
48 
49         // The first parameter, required, has no default value assigned
50         // to it. Therefore, it is not optional. Both optionalstr and 
51         // optionalint have default values assigned to them. They are optional.
52         public void ExampleMethod(int required, string optionalstr = "default string",
53             int optionalint = 10)
54         {
55             Console.WriteLine("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
56                 optionalint);
57         }
58     }
59 
60     // The output from this example is the following:
61     // Default name: 1, One, and 1.
62     // Default name: 2, Two, and 10.
63     // Default name: 3, default string, and 10.
64     // Provided name: 1, One, and 1.
65     // Provided name: 2, Two, and 10.
66     // Provided name: 3, default string, and 10.
67     // Default name: 3, default string, and 4.
68 
69 }

 

posted @ 2013-05-02 11:45  Geodon  阅读(395)  评论(0编辑  收藏  举报