Visual C# 2010 引入了命名實參和可選實參。利用“命名實參”,您將能夠為特定形參指定實參,方法是將實參與該形參的名稱關聯(lián),而不是與形參在形參列表中的位置關聯(lián)。利用“可選實參”,您將能夠為某些形參省略實參。這兩種技術都可與方法、索引器、構造函數(shù)和委托一起使用。
在使用命名實參和可選實參時,將按實參出現(xiàn)在實參列表(而不是形參列表)中的順序計算這些實參。
命名形參和可選形參一起使用時,您將能夠只為可選形參列表中的少數(shù)形參提供實參。此功能大大方便了對 COM 接口(例如 Microsoft Office 自動化 API)的調用。
命名實參
有了命名實參,您將不再需要記住或查找形參在所調用方法的形參列表中的順序。可以按形參名稱指定每個實參的形參。例如,可以采用標準方式調用計算身體質量指數(shù) (BMI) 的函數(shù),方法是依照該函數(shù)定義的順序按位置發(fā)送體重和身高的實參。
CalculateBMI(123, 64);
如果不記得形參的順序,但卻知道其名稱,您可以按任意順序(先發(fā)送體重或先發(fā)送身高)發(fā)送實參。
CalculateBMI(weight: 123, height: 64);CalculateBMI(height: 64, weight: 123);
命名實參還可以標識每個實參所表示的含義,從而改進代碼的可讀性。
命名實參可以放在位置實參后面,如此處所示。
CalculateBMI(123, height: 64);
但是,位置實參不能放在命名實參后面。下面的語句會導致編譯器錯誤。
CalculateBMI(weight: 123, 64);
示例
下面的代碼實現(xiàn)本節(jié)中的示例。
class NamedExample{ static void Main(string[] args) { // The method can be called in the normal way, by using positional arguments. Console.WriteLine(CalculateBMI(123, 64)); // Named arguments can be supplied for the parameters in either order. Console.WriteLine(CalculateBMI(weight: 123, height: 64)); Console.WriteLine(CalculateBMI(height: 64, weight: 123)); // Positional arguments cannot follow named arguments. // The following statement causes a compiler error. //Console.WriteLine(CalculateBMI(weight: 123, 64)); // Named arguments can follow positional arguments. Console.WriteLine(CalculateBMI(123, height: 64)); } static int CalculateBMI(int weight, int height) { return (weight * 703) / (height * height); }}可選參數(shù)
方法、構造函數(shù)、索引器或委托的定義可以指定其形參為必需還是可選。任何調用都必須為所有必需的形參提供實參,但可以為可選的形參省略實參。
每個可選形參都具有默認值作為其定義的一部分。如果沒有為該形參發(fā)送實參,則使用默認值。默認值必須是一個表達式的以下類型之一:
常數(shù)表達式;
窗體 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 使用括號指示可選形參,如下圖所示。

示例
在下面的示例中,ExampleClass 的構造函數(shù)具有一個形參,該形參是可選的。實例方法 ExampleMethod 具有一個必需的形參:required,以及兩個可選形參:optionalstr 和 optionalint。 Main 中的代碼演示了可用于調用構造函數(shù)和方法的不同方式。
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); } }}輸出:
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.
COM 接口
命名實參和可選實參,以及對動態(tài)對象的支持和其他增強功能大大提高了與 COM API(例如 Office 自動化 API)的互操作性。
例如,Microsoft Office Excel 的 Range 接口中的 AutoFormat 方法具有七個形參,這七個形參都是可選的。這些形參如下圖所示。

在 C# 3.0 和早期版本中,每個形參都需要一個實參,如以下示例所示。
// In C# 3.0 and earlier versions, you need to supply an argument for// every parameter. The following call specifies a value for the first// parameter, and sends a placeholder value for the other six. The// default values are used for those parameters.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);但是,可以通過使用 C# 4.0 中引入的命名實參和可選實參來大大簡化對 AutoFormat 的調用。如果不希望更改形參的默認值,則可以通過使用命名實參和可選實參來為可選形參省略實參。在下面的調用中,僅為七個形參中的其中一個指定了值。
// The following code shows the same call to AutoFormat in C# 4.0. Only// the argument for which you want to provide a specific value is listed.excelApp.Range["A1", "B4"].AutoFormat( Format: myFormat );
重載決策
使用命名實參和可選實參將在以下方面對重載決策產生影響:
如果方法、索引器或構造函數(shù)的各個形參均為可選,或者按名稱或位置與調用語句中的單個實參對應,并且該實參可轉換為形參的類型,則該方法、索引器或構造函數(shù)是執(zhí)行的候選項。
如果找到多個候選項,則會將首選轉換的重載決策規(guī)則應用于顯式指定的實參。將忽略可選形參已省略的實參。
如果兩個候選項不相上下,則會將沒有可選形參的候選項作為首選項,對于這些可選形參,已在調用中為其省略了實參。這是具有較少形參的候選項的重載決策中一般首選項的結果。
新聞熱點
疑難解答