简体   繁体   English

C# 4 中的重载分辨率和可选参数

[英]Overload Resolution and Optional Parameters in C# 4

I am working with some code that has seven overloads of a function TraceWrite :我正在处理一些具有函数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: 1) 在其他地方,我使用四个参数调用TraceWritestring, 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 doesn't this call resolve to the second overload?为什么此调用不解析为第二个重载? ( TraceWrite(string, LogLevelENUM, string, bool, string = "") ) ( TraceWrite(string, LogLevelENUM, string, bool, string = "") )

2) If I were to call TraceWrite with string, LogLevelENUM, string, string , which overload would be called? 2) 如果我用string, LogLevelENUM, string, string调用TraceWrite ,会调用哪个重载? The first or the third?第一还是第三? And why?为什么?

编译器将选择重载#1,因为它与参数数量和签名完全匹配。

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. 第三个参数应该没有最后一个参数的默认值,第一个参数应该在最后一个字符串之前具有不同的非字符串参数,或者第三个参数的PieceID参数应为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. 希望您可以将重载次数减少到1或2。

public static int add(int a = 0, int b = 0)
{
    return a + b;
}
add(b: 1);

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:如果您希望将其解析为第三个重载,请在调用中使用命名参数,例如:

TraceWrite("string", LogLevelENUM.Level, "string", PieceID: "string");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM