#815 – Named vs. Positional Arguments

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);
Advertisement