|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
Benannte und optionale Argumente (C#-Programmierhandbuch)
CalculateBMI(123, 64);
CalculateBMI(weight: 123, height: 64);
CalculateBMI(height: 64, weight: 123);
CalculateBMI(123, height: 64);
//CalculateBMI(weight: 123, 64);
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); } }
-
ein konstanter Ausdruck. -
Ein Ausdruck der Form new ValType(), in dem ValType ein Werttyp ist, z. B. Enumeration oder Struktur. -
Ein Ausdruck der Form ValType (Standard), in dem ValType ein Werttyp ist.
public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10)
//anExample.ExampleMethod(3, ,4);
anExample.ExampleMethod(3, optionalint: 4);
Hinweis
|
|---|
|
|
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. }
// 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);
// 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 );
-
Eine Methode, ein Indexer oder ein Konstruktor kommt für die Ausführung in Frage, wenn jeder der zugehörigen Parameter entweder optional ist oder anhand von Name oder Position einem einzelnen Argument in der aufrufenden Anweisung zugewiesen ist, das in den Typ des Parameters konvertiert werden kann. -
Bei mehreren möglichen Kandidaten werden die Überladungsauflösungsregeln für bevorzugte Konvertierungen auf die explizit angegebenen Argumente angewendet. Weggelassene Argumente für optionale Parameter werden ignoriert. -
Wenn zwei Kandidaten gleich gut geeignet sind, wird der Kandidat bevorzugt, der keine optionalen Parameter aufweist, für die Argumente weggelassen wurden. Die Ursache hierfür ist eine allgemeine Einstellung für die Überladungsauflösung für Kandidaten mit weniger Parametern.
Weitere Informationen finden Sie in der C#-Sprachspezifikation. Die Sprachspezifikation ist die verbindliche Quelle für die Syntax und Verwendung von C#.
Hinweis