#815 – Named vs. Positional Arguments
April 4, 2013 3 Comments
C# supports the use of named arguments, in which you can change the order of arguments passed to a function, by prefixing an argument with the corresponding parameter name.
An argument that is not prefixed with the name of a parameter is considered a positional argument.
Positional arguments must come before named arguments and match the order of the corresponding parameters.
// Sample method with 3 parameters public void Bark(int numTimes, string sound, double volume)
Below are some examples of using both named and positional arguments.
// Example 1: All arguments are positional myDog.Bark(2, "Woof", 10.0); // Example 2: Only 1st argument is positional myDog.Bark(2, volume: 10.0, sound: "Woof"); // Example 3: All arguments are named myDog.Bark(volume: 10.0, sound: "Woof", numTimes: 2);
Pingback: Dew Drop – April 4, 2013 (#1,520) | Alvin Ashcraft's Morning Dew
Why is that positional arguments are placed before named arguments and even if we follow same order in method calling as in definition why can’t we place positional arguments after named arguments?
Simple and awesome