C# : overloading constructors with optional parameters & named arguments?

25,632

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

Share:
25,632
user1229895
Author by

user1229895

Updated on January 15, 2020

Comments

  • user1229895
    user1229895 over 4 years

    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();
    
  • user1229895
    user1229895 about 12 years
    I understand that it compares the signatures, but could you rephrase my example code to make it work?
  • Yinda Yin
    Yinda Yin about 12 years
    How does it "not work?" Does it call the wrong overload? Does it throw an exception?
  • user1229895
    user1229895 about 12 years
    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
    user1229895 about 12 years
    throws an exception...error CS1739 the best overload doesn't have a parameter 'num'
  • Yinda Yin
    Yinda Yin about 12 years
    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.
  • user1229895
    user1229895 about 12 years
    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? ;-)