深入解析C#中的命名實參和可選實參
Visual C# 2010 引入了命名實參和可選實參。利用“命名實參”,您將能夠為特定形參指定實參,方法是將實參與該形參的名稱關(guān)聯(lián),而不是與形參在形參列表中的位置關(guān)聯(lián)。利用“可選實參”,您將能夠為某些形參省略實參。這兩種技術(shù)都可與方法、索引器、構(gòu)造函數(shù)和委托一起使用。
在使用命名實參和可選實參時,將按實參出現(xiàn)在實參列表(而不是形參列表)中的順序計算這些實參。
命名形參和可選形參一起使用時,您將能夠只為可選形參列表中的少數(shù)形參提供實參。此功能大大方便了對 COM 接口(例如 Microsoft Office 自動化 API)的調(diào)用。
命名實參
有了命名實參,您將不再需要記住或查找形參在所調(diào)用方法的形參列表中的順序。可以按形參名稱指定每個實參的形參。例如,可以采用標(biāo)準(zhǔn)方式調(diào)用計算身體質(zhì)量指數(shù) (BMI) 的函數(shù),方法是依照該函數(shù)定義的順序按位置發(fā)送體重和身高的實參。
CalculateBMI(123, 64);
如果不記得形參的順序,但卻知道其名稱,您可以按任意順序(先發(fā)送體重或先發(fā)送身高)發(fā)送實參。
CalculateBMI(weight: 123, height: 64); CalculateBMI(height: 64, weight: 123);
命名實參還可以標(biāo)識每個實參所表示的含義,從而改進(jìn)代碼的可讀性。
命名實參可以放在位置實參后面,如此處所示。
CalculateBMI(123, height: 64);
但是,位置實參不能放在命名實參后面。下面的語句會導(dǎo)致編譯器錯誤。
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ù)
方法、構(gòu)造函數(shù)、索引器或委托的定義可以指定其形參為必需還是可選。任何調(diào)用都必須為所有必需的形參提供實參,但可以為可選的形參省略實參。
每個可選形參都具有默認(rèn)值作為其定義的一部分。如果沒有為該形參發(fā)送實參,則使用默認(rèn)值。默認(rèn)值必須是一個表達(dá)式的以下類型之一:
常數(shù)表達(dá)式;
窗體 new ValType()的表達(dá)式, ValType 是值類型,例如 枚舉 或 結(jié)構(gòu);
窗體 默認(rèn) (ValType)的表達(dá)式, ValType 是值類型。
可選形參在形參列表的末尾定義,位于任何必需的形參之后。如果調(diào)用方為一系列可選形參中的任意一個形參提供了實參,則它必須為前面的所有可選形參提供實參。實參列表中不支持使用逗號分隔的間隔。例如,在以下代碼中,使用一個必選形參和兩個可選形參定義實例方法 ExampleMethod。
public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10)
下面對 ExampleMethod 的調(diào)用導(dǎo)致編譯器錯誤,原因是為第三個形參而不是為第二個形參提供了實參。
anExample.ExampleMethod(3, ,4);
但是,如果您知道第三個形參的名稱,則可以使用命名實參來完成任務(wù)。
anExample.ExampleMethod(3, optionalint: 4);
IntelliSense 使用括號指示可選形參,如下圖所示。
示例
在下面的示例中,ExampleClass 的構(gòu)造函數(shù)具有一個形參,該形參是可選的。實例方法 ExampleMethod 具有一個必需的形參:required,以及兩個可選形參:optionalstr 和 optionalint。 Main 中的代碼演示了可用于調(diào)用構(gòu)造函數(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)對象的支持和其他增強(qiáng)功能大大提高了與 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 的調(diào)用。如果不希望更改形參的默認(rèn)值,則可以通過使用命名實參和可選實參來為可選形參省略實參。在下面的調(diào)用中,僅為七個形參中的其中一個指定了值。
// 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 );
重載決策
使用命名實參和可選實參將在以下方面對重載決策產(chǎn)生影響:
如果方法、索引器或構(gòu)造函數(shù)的各個形參均為可選,或者按名稱或位置與調(diào)用語句中的單個實參對應(yīng),并且該實參可轉(zhuǎn)換為形參的類型,則該方法、索引器或構(gòu)造函數(shù)是執(zhí)行的候選項。
如果找到多個候選項,則會將首選轉(zhuǎn)換的重載決策規(guī)則應(yīng)用于顯式指定的實參。將忽略可選形參已省略的實參。
如果兩個候選項不相上下,則會將沒有可選形參的候選項作為首選項,對于這些可選形參,已在調(diào)用中為其省略了實參。這是具有較少形參的候選項的重載決策中一般首選項的結(jié)果。
- 一道關(guān)于C#參數(shù)傳遞的面試題分析
- C#實現(xiàn)向函數(shù)傳遞不定參數(shù)的方法
- C#傳遞參數(shù)到線程的方法匯總
- C#中的多線程多參數(shù)傳遞詳解
- c#線程間傳遞參數(shù)詳解
- C#和asp.net中鏈接數(shù)據(jù)庫中參數(shù)的幾種傳遞方法實例代碼
- C# 運(yùn)用params修飾符來實現(xiàn)變長參數(shù)傳遞的方法
- c#方法中調(diào)用參數(shù)的值傳遞方式和引用傳遞方式以及ref與out的區(qū)別深入解析
- C#難點(diǎn)逐個擊破(1):ref參數(shù)傳遞
- asp.net(C#)函數(shù)對象參數(shù)傳遞的問題
- C# 使用匿名函數(shù)解決EventHandler參數(shù)傳遞的難題
- 理解C#中參數(shù)的值和引用以及傳遞結(jié)構(gòu)和類引用的區(qū)別
相關(guān)文章
C#12中的Primary?Constructors主構(gòu)造函數(shù)詳解
主構(gòu)造函數(shù)把參數(shù)添加到class與record的類聲明中就是主構(gòu)造函數(shù),這篇文章主要介紹了C#12中的Primary?Constructors 主構(gòu)造函數(shù),需要的朋友可以參考下2023-11-11c#循環(huán)中產(chǎn)生偽隨機(jī)數(shù)
在循環(huán)中產(chǎn)生多個隨機(jī)數(shù),容易出現(xiàn)連續(xù)相同的數(shù)據(jù),最終的多個隨機(jī)數(shù)并不隨機(jī),而是帶有某種規(guī)律性。2010-06-06C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的實現(xiàn)方法
這篇文章主要介紹了C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的實現(xiàn)方法,詳細(xì)講述了C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的原理與實現(xiàn)技巧,需要的朋友可以參考下2014-10-10