#593 – Using Named Arguments

Traditionally in C#, arguments must be passed in to a method in the same order in which they are defined in the method’s declaration.

public void DoATrick(string trickName, int numTimes)
            Dog kirby = new Dog("Kirby");

            kirby.DoATrick("Fetch", 158);

You can, however, change the order of the arguments by preceding each value by the name of the parameter, with a colon (:) following each name.

            kirby.DoATrick(numTimes: 5, trickName: "Run around me");

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #593 – Using Named Arguments

  1. Pingback: #594 – When You’d Want to Use Named Arguments « 2,000 Things You Should Know About C#

  2. Pingback: #693 – Named Arguments in Constructors Allow the Most Flexibility « 2,000 Things You Should Know About C#

  3. Pingback: #815 – Named vs. Positional Arguments | 2,000 Things You Should Know About C#

  4. Pingback: #816 – Named Argument and Optional Parameter Combinations | 2,000 Things You Should Know About C#

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: