C#4.0引入了命名实参和可选实参。
命名实参就是可以使用形参名:参数进行传递。
可选实参就是在声明形参时给定一个默认值,那么这个参数就是可选实参,可选参数可以被省略,则为默认值。
两种技术都可与方法、索引器、构造函数和委托一起使用。
class Product
{ public static void show(string name,string address)
{
Console.WriteLine("我的名字叫:{0},来自{1}",name,address);
}
}
class Program
{
static void Main(string[] args)
{
Product.show(address: "QNMLGB", name: "呵呵");
Product.show("呵呵",address: "QNMLGB");//反过来会导致编译错误。
} }
命名实参:
使用命名实参的好处:
1、如果你不记得参数的位置可以直接使用参数名冒号参数进行传递。
2、可改进代码的可读性。
3、可以摆脱为可选实参赋值后前面的可选实参也要赋值的问题。
注意:
当命名实参与位置实参公用时,应把位置实参放在最前,后面放命名实参,否则会导致编译错误。
可选实参:方法、索引器、构造函数和委托其定义都可以指定其形参是为必须还是可选的,调用时必须为必须实参给定参数,而可选实参可以省略。每个可选形参都具有默认值,如果没有为可选形参给定实参,则使用默认值。
注意:1、可选形参必须要在所有必选形参之后定义。
2、如果为可选实参赋值并且使用位置实参的方式进行赋值时,必须为前面的可选实参进行赋值。
3、可选实参不能使用逗号作为分隔的间隔。举个例子吧:,正确方式为:
4、可以使用命名实参来避免使用位置实参(要对可选实参的前面的形参赋值)。
5、还可以通过使用 .NET OptionalAttribute 类来声明可选形参。
OptionalAttribute 形参不需要默认值。示例在下面的示例中,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);
}
}
// The output from this example is the following:
// Default name: 1, One, and 1.
// Default name: 2, Two, and 10.
// Default name: 3, default string, and 10.
// Provided name: 1, One, and 1.
// Provided name: 2, Two, and 10.
// Provided name: 3, default string, and 10.
// Default name: 3, default string, and 4.
重载决策
使用命名实参和可选实参将在以下方面对重载决策产生影响:如果方法、索引器或构造函数的各个形参均为可选,或者按名称或位置与调用语句中的单个实参对应,并且该实参可转换为形参的类型,则该方法、索引器或构造函数是执行的候选项。如果找到多个候选项,则会将首选转换的重载决策规则应用于显式指定的实参。 将忽略可选形参已省略的实参。如果两个候选项不相上下,则会将没有可选形参的候选项作为首选项,对于这些可选形参,已在调用中为其省略了实参。 这是具有较少形参的候选项的重载决策中一般首选项的结果。