Tag: overload resolution

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 […]

重载,generics类型推断和’params’关键字

我刚注意到一个带有重载决策的奇怪行为。 假设我有以下方法: public static void DoSomething(IEnumerable items) { // Whatever // For debugging Console.WriteLine(“DoSomething(IEnumerable items)”); } 现在,我知道这个方法通常会使用少量显式参数调用,所以为方便起见,我添加了这个重载: public static void DoSomething(params T[] items) { // Whatever // For debugging Console.WriteLine(“DoSomething(params T[] items)”); } 现在我尝试调用这些方法: var items = new List { “foo”, “bar” }; DoSomething(items); DoSomething(“foo”, “bar”); 但在这两种情况下,都会调用params的重载。 我希望在List的情况下调用IEnumerable重载,因为它看起来更匹配(至少对我而言)。 这种行为是否正常? 谁能解释一下呢? 我在MSDN文档中找不到任何关于它的明确信息……这里涉及的重载决策规则是什么?

为什么C#编译器重载解析算法将具有相同签名的静态和实例成员视为相等?

让我们有两个成员相同的签名,但一个是静态的而另一个是 – 不是: class Foo { public void Test() { Console.WriteLine(“instance”); } public static void Test() { Console.WriteLine(“static”); } } 但是这样的代码生成会带来编译错误: 类型’Foo’已经定义了一个名为’Test’的成员,它具有相同的参数类型 但为什么? 让我们成功编译,然后: Foo.Test()应输出“static” new Foo().Test(); 应输出“实例” 无法调用静态成员而不是实例1,因为在这种情况下会出现另一个更合理的编译器错误: 无法使用实例引用访问成员’Foo.Test()’; 用类型名称来限定它