#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);
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #815 – Named vs. Positional Arguments

  1. Pingback: Dew Drop – April 4, 2013 (#1,520) | Alvin Ashcraft's Morning Dew

  2. Akshay Bhardwaj says:

    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?

  3. ajithraju says:

    Simple and awesome

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: