C# 4 中的重载解析和可选参数

发布于 2024-09-05 17:18:00 字数 1597 浏览 15 评论 0原文

我正在使用一些代码,该代码具有函数 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) 在其他地方,我使用四个参数调用 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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦情居士 2024-09-12 17:18:00

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

The compiler will choose overload #1 because it has an exact match for the number of parameters and the signature.

救星 2024-09-12 17:18:00

你的过载很糟糕,你应该在它们之间做出更多的区别。编译器无法知道您的意思是第一个还是第三个。

第三个参数的最后一个参数应该没有默认值,第一个参数在最后一个字符串之前应该有一个不同的非字符串参数,或者第三个参数的 PieceID 参数应该是 int。

有一个不同的可能更好的解决方案:使用多个默认值。您有如此多的默认值,它们应该减少重载的数量。使用多个默认值,您可以仅指定最后一个值来调用方法。希望您可以将重载次数减少到 1 或 2 次。

public static int add(int a = 0, int b = 0)
{
    return a + b;
}
add(b: 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.

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.

public static int add(int a = 0, int b = 0)
{
    return a + b;
}
add(b: 1);
安穩 2024-09-12 17:18:00

对于第二个问题,您的调用将解析为第一个重载,因为它的签名具有较少的参数。

如果您希望将其解析为第三个重载,请在调用中使用命名参数,例如如下所示:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文