8

This isn't a question on proper coding practice, I'm just working through the semantics. lets say I have the following constructors...

public FooClass(string name = "theFoo")
{ fooName = name; }

public FooClass(string name, int num = 7, bool boo = true) : this(name)
{ fooNum = num; fooBool = boo; }

is it possible to use named arguments thusly...?

FooClass foo1 = new FooClass(num:1);  

// where I'm only passing one named argument, relying on the optionals to take care of the rest

or call the constructor FooClass(string, int, bool) with no arguments? as in...

FooClass foo2 = new FooClass();
j0k
  • 22,600
  • 28
  • 79
  • 90
user1229895
  • 2,259
  • 8
  • 24
  • 26

2 Answers2

8

Use of named and optional arguments affects overload resolution in the following ways:

  • A method, indexer, or constructor is a candidate for execution if each of its parameters either is optional or corresponds, by name or by position, to a single argument in the calling statement, and that argument can be converted to the type of the parameter.

  • If more than one candidate is found, overload resolution rules for preferred conversions are applied to the arguments that are explicitly specified. Omitted arguments for optional parameters are ignored.

  • If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.

http://msdn.microsoft.com/en-us/library/dd264739.aspx

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I understand that it compares the signatures, but could you rephrase my example code to make it work? – user1229895 Feb 24 '12 at 04:40
  • How does it "not work?" Does it call the wrong overload? Does it throw an exception? – Robert Harvey Feb 24 '12 at 04:42
  • sorry entered too soon... so for the first case, without explicitly stating an argument all optionals are ignored, resulting in the lesser signature being called... and in the second case of calling the overload with FooClass(), it's not possible... so the answer is it is not possible to use named with omitted optional arguments? – user1229895 Feb 24 '12 at 04:46
  • throws an exception...error CS1739 the best overload doesn't have a parameter 'num' – user1229895 Feb 24 '12 at 04:47
  • 2
    To get it to call the overload with the `num` parameter in it, you also have to supply the `name` parameter. Since you didn't specify an optional value for the `name` parameter in the second overload, *it is not an optional parameter.* – Robert Harvey Feb 24 '12 at 04:50
  • i see now, the optional name value didn't carry over to the overload didn't carry over. thank you Robert! btw, how do I add rep? ;-) – user1229895 Feb 24 '12 at 05:24
0

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported.

Also,

A named argument can follow positional arguments, as shown here. CalculateBMI(123, height: 64); However, a positional argument cannot follow a named argument. The following statement causes a compiler error. //CalculateBMI(weight: 123, 64);