重载解析和C#4中的可选参数 [英] Overload Resolution and Optional Parameters in C# 4

查看:198
本文介绍了重载解析和C#4中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与有七个重载函数 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.)

所以,与背景:结果
1)在其他地方,我称之为 TraceWrite 用四个参数:字符串,LogLevelENUM,字符串,布尔,我也得到了以下错误:

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'

为什么没有这个调用决心第二超载? ( TraceWrite(字符串,LogLevelENUM,字符串,布尔,字符串=)

Why doesn't this call resolve to the second overload? (TraceWrite(string, LogLevelENUM, string, bool, string = ""))

2)如果我是调用 TraceWrite 字符串,LogLevelENUM,字符串,字符串,其中超载会叫什么名字?第一或第三?为什么?

2) If I were to call TraceWrite with string, LogLevelENUM, string, string, which overload would be called? The first or the third? And why?

推荐答案

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

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

这篇关于重载解析和C#4中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆