Tag: 重载决策

使用变量generics委托类型对operator ==进行重载解析

在两个委托类型表达式之间使用==进行重载解析的精确规则是什么? 请考虑以下代码(需要using System; ): static class ProgramA { static void TargetMethod(object obj) { } static void Main() { Action instance1 = TargetMethod; Action instance2 = TargetMethod; Action a1 = instance1; Action a2 = instance2; Console.WriteLine((object)a1 == (object)a2); Console.WriteLine((Delegate)a1 == (Delegate)a2); Console.WriteLine((Action)a1 == (Action)a2); Console.WriteLine(a1 == a2); // warning CS0253: Possible unintended reference comparison; to get […]

关于generics和IEnumerable的方法重载解析

前几天我注意到了这一点,说你有两个重载方法: public void Print(IEnumerable items) { Console.WriteLine(“IEnumerable T”); } public void Print(T item) { Console.WriteLine(“Single T”); } 这段代码: public void TestMethod() { var persons = new[] { new Person { Name = “Yan”, Age = 28 }, new Person { Name = “Yinan”, Age = 28 } }; Print(persons); Print(persons.ToList()); } 打印: Single T Single […]