开发者

C# Default optional parameter chooser

    public ClassType(string type) {
        Type = type;
    }
    public ClassType(string type,bool isArray=false) {
        Type = type;
        IsArray = isArray;
    }


    ClassType ct = new ClassType("adsf"开发者_如何学JAVA);

Which constructor is chosen?


The overload that doesn't require an optional parameter. Note that it's just a "yes" or "no" decision here: "no optional parameters filled in automatically" is preferable to "some optional parameters filled in automatically" but there's no preference between 1 or 2 being filled in. (That would be ambiguous.)

From section 7.5.3.2 of the C# 4 spec:

Otherwise if all parameters of MP have a corresponding argument whereas default arguments need to be substituted for at least one optional parameter in MQ then MP is better than MQ.


As Jon said, in two words, the first one. The match is 'cleaner'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜