54

I've begun to notice at times when I'm making method calls in C# that the names of the parameters for the method I'm calling will show up in the intellisense list appended with a colon, and that I can then format the method call thusly:

MethodCall(parameter1:value1, parameter2:value2);

Is this a new language feature? It reminds me of the way you can call stored procedures in SQL and specify parameter names like so:

spDoSomeStuff @param1 = 1, @param2 = 'other param'

Is this a similar feature? If so, to what end? If not, what is it and what is it to be used for.

1
  • This seems more or less like how it's been implemented in Python, where you can explicitly specify parameters you want to call using method(param="value") syntax
    – Lou
    Commented Jan 27, 2023 at 9:44

5 Answers 5

49

It's a new feature. See here: http://msdn.microsoft.com/en-us/library/dd264739.aspx Named parameters are standard in ObjectiveC for instance. It takes some time to get used to them but they are a good thing. Only from looking you can tell what a parameter is meant for.

1
  • 10
    I can see a lot of value in having the parameter name immediately visible without having to navigate to the method's definition or let intellisense tell you what they all are. Commented Mar 10, 2011 at 16:35
28

It is worth mentioning, unlike optional parameters, you can skip certain arguments and pass only the parameters you are interested in.

public void Example(int required, string StrVal = "default", int IntVal = 0)
{
    // ...
}

public void Test()
{
    // This gives compiler error
    // Example(1, 10);

    // This works
    Example(1, IntVal:10);
}
24

Named parameters allow you explicitly set the value of arguments in a custom order independent of the signature. Method signatures are defined by the argument types, ie, Foo( int i, bool b ), which will only accept arguments of type int and bool in that order. Named arguments allow you to pass b first and i second.

4
  • 2
    I'm familiar with the concept of a method's signature and I wondered whether this would allow you to change the order of parameters in your call. I'm curious though - what's the point of being able to change the order? Commented Mar 10, 2011 at 16:34
  • 1
    It's really just a code "readability" preference. The method signature is static in that when it's called the arguments will be placed on the stack in the order they are defined in the method signature. The "Named Arguments" feature just allows you to rearrange the arguments to your preference. Commented Mar 10, 2011 at 16:40
  • 3
    @Zannjaminderson To me, the real value comes when you have methods with many default values. If you only need to change some of them you can pick them out using named parameters. This way you don't have to restate all the default values preceding the one you want to change. Commented May 26, 2013 at 9:55
  • @Zannjaminderson I was actually just discussing that point as a benefit when explaining this concept to some coworkers a couple weeks ago. No idea why I didn't include it in the answer as it's definitely a huge benefit. +1 Commented May 28, 2013 at 17:01
6

Scott Gu has introduced this new feature in his blog:

Optional Parameters and Named Arguments in C# 4

3

It's the Named and Optional Parameters that came in with C# 4.

Not the answer you're looking for? Browse other questions tagged or ask your own question.