C# 默认可选参数选择器
public ClassType(string type) {
Type = type;
}
public ClassType(string type,bool isArray=false) {
Type = type;
IsArray = isArray;
}
ClassType ct = new ClassType("adsf");
选择哪个构造函数?
public ClassType(string type) {
Type = type;
}
public ClassType(string type,bool isArray=false) {
Type = type;
IsArray = isArray;
}
ClassType ct = new ClassType("adsf");
Which constructor is chosen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要可选参数的重载。请注意,这里只是一个“是”或“否”的决定:“不自动填写可选参数”比“自动填写一些可选参数”更好,但在填写 1 或 2 之间没有偏好。(这将是不明确。)
来自 C# 4 规范的第 7.5.3.2 节:
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:
正如乔恩所说,用两个词来说,第一个。比赛更加“干净”。
As Jon said, in two words, the first one. The match is 'cleaner'.