我正在处理一些代码,该代码具有函数 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 = "");
(所有公共(public)静态、命名空间噪音从头到尾都被忽略了。)
所以,有了这样的背景:
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'
为什么这个调用没有解析到第二个重载? (TraceWrite(string, LogLevelENUM, string, bool, string = "")
)
2) 如果我用string, LogLevelENUM, string, string
调用TraceWrite
,会调用哪个重载?第一还是第三?为什么?
最佳答案
编译器将选择重载#1,因为它的参数数量和签名完全匹配。
关于c# - C# 4 中的重载分辨率和可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3061925/