Constructor overload contains already defines a member with the same signature

后端 未结 3 1918
温柔的废话
温柔的废话 2021-01-28 05:48
public Module(string a, object obj) : this(a, null, obj) { }

public Module(string b, object obj) : this(null, b, obj) { }

These constructor overloads

相关标签:
3条回答
  • 2021-01-28 06:11

    Parameter names are meaningless in the context of overloads. I can see what you are trying to do, but I'm not sure why. I would dispense with it entirely:

    public Module(string a, string b, object obj){}
    

    Then call the Module constructor, passing in null values as appropriate.

    Module m = new Module(null, "hi", obj);
    
    Module m2 = new Module("bye", null, obj);
    
    0 讨论(0)
  • 2021-01-28 06:13

    personally i use optional arguments:

    public Module(object obj, string a = null,string b = null) : this(a, b, obj) { }
    

    Heres an article on what they are and how to use them, Note though they are only avaliable in VS2010 (they are part of the compiler not the language so they are available in .net 3.5 as well as 4.0) http://msdn.microsoft.com/en-us/library/dd264739.aspx

    0 讨论(0)
  • 2021-01-28 06:31

    It's just not possible. You should remove those two constructor overloads.

    However, you could work with static factory methods instead. Make sure to use clear names to convey the difference to the API consumer.

    static Module CreateA(string a, object o) { return new Module(a, null, o); }
    static Module CreateB(string b, object o) { return new Module(null, b, o); }
    
    0 讨论(0)
提交回复
热议问题