C# 4 中的重载解析和可选参数
我正在使用一些代码,该代码具有函数 TraceWrite
的七个重载:(
void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, bool LogToFileOnly, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string PieceID, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, LogWindowCommandENUM LogWindowCommand, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, bool UserMessage, int UserMessagePercent, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string PieceID, LogWindowCommandENUM LogWindowCommand, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, LogWindowCommandENUM LogWindowCommand, bool UserMessage, int UserMessagePercent, string Data = "");
上面和整个过程中都忽略了所有公共静态、命名空间噪音。)
因此,有了这个背景:
1) 在其他地方,我使用四个参数调用 TraceWrite
:string、LogLevelENUM、string、bool
,并且出现以下错误:
error CS1502: The best overloaded method match for 'TraceWrite(string, LogLevelENUM, string, string)' has some invalid arguments error CS1503: Argument '4': cannot convert from 'bool' to 'string'
Why isn't this call returned to the second超载? (TraceWrite(string, LogLevelENUM, string, bool, string = "")
)
2) 如果我用 string, LogLevelENUM, string, string 调用
,哪个重载会被调用?第一个还是第三个?为什么?TraceWrite
I am working with some code that has seven overloads of a function TraceWrite
:
void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, bool LogToFileOnly, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string PieceID, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, LogWindowCommandENUM LogWindowCommand, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, bool UserMessage, int UserMessagePercent, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, string PieceID, LogWindowCommandENUM LogWindowCommand, string Data = ""); void TraceWrite(string Application, LogLevelENUM LogLevel, string Message, LogWindowCommandENUM LogWindowCommand, bool UserMessage, int UserMessagePercent, string Data = "");
(All public static, namespacing noise elided above and throughout.)
So, with that background:
1) Elsewhere, I call TraceWrite
with four arguments: string, LogLevelENUM, string, bool
, and I get the following errors:
error CS1502: The best overloaded method match for 'TraceWrite(string, LogLevelENUM, string, string)' has some invalid arguments error CS1503: Argument '4': cannot convert from 'bool' to 'string'
Why doesn't this call resolve to the second overload? (TraceWrite(string, LogLevelENUM, string, bool, string = "")
)
2) If I were to call TraceWrite
with string, LogLevelENUM, string, string
, which overload would be called? The first or the third? And why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译器将选择重载#1,因为它的参数数量和签名完全匹配。
The compiler will choose overload #1 because it has an exact match for the number of parameters and the signature.
你的过载很糟糕,你应该在它们之间做出更多的区别。编译器无法知道您的意思是第一个还是第三个。
第三个参数的最后一个参数应该没有默认值,第一个参数在最后一个字符串之前应该有一个不同的非字符串参数,或者第三个参数的 PieceID 参数应该是 int。
有一个不同的可能更好的解决方案:使用多个默认值。您有如此多的默认值,它们应该减少重载的数量。使用多个默认值,您可以仅指定最后一个值来调用方法。希望您可以将重载次数减少到 1 或 2 次。
Your overloads are bad, you should make more difference between them. The compiler cannot know whether you meant the first or the third.
Either the third should have no default value for its last argument, the first should have a different non-string argument before the last string or the PieceID argument of the third should be an int.
There is a different possible better solution: use multiple defaults. You have so many defaults they should reduce the number of overloads. With multiple default values you can call a method with only specifiying the last value. Hopefully you can reduce the number of overloads to 1 or 2.
对于第二个问题,您的调用将解析为第一个重载,因为它的签名具有较少的参数。
如果您希望将其解析为第三个重载,请在调用中使用命名参数,例如如下所示:
For the second question, your call would be resolved to the first overload, because its signature has fewer parameters.
If you'd like it to be resolved to the third overload, then use named arguments in your call, for example like this: