Tag: 重载

访问重写方法

有一个练习“OverloadResolutionOverride” 以下代码的输出结果如何: class Foo { public virtual void Quux(int a) { Console.WriteLine(“Foo.Quux(int)”); } } class Bar : Foo { public override void Quux(int a) { Console.WriteLine(“Bar.Quux(int)”); } public void Quux(object a) { Console.WriteLine(“Bar.Quux(object)”); } } class Baz : Bar { public override void Quux(int a) { Console.WriteLine(“Baz.Quux(int)”); } public void Quux(params T[] a) { Console.WriteLine(“Baz.Quux(params […]

使用参数Func 重载方法

我想创建一些接受Func参数的重载方法。 重载方法应该使用参数中定义的最通用类型调用该方法。 下面是我的方法的一个简单示例,以及我想如何调用它们: public static TResult PerformCaching(Func func, T1 first, string cacheKey) { return PerformCaching((t, _, _) => func, first, null, null, cacheKey); } public static TResult PerformCaching(Func func, T1 first, T2 second, string cacheKey) { return PerformCaching((t, t2, _) => func, first, second, null, cacheKey); } public static TResult PerformCaching(Func func, T1 first, T2 […]

为什么比较运算符不会自动超载IComparable?

当一个类是IComparable ,由于CompareTofunction,我们知道所有重载< , >和==运算符的东西,对吧? 那么为什么这些不会自动超载? 看看下面的例子: public class MyComparable : IComparable { public int Value { get; } public MyComparable(int value) { Value = value; } public int CompareTo(MyComparable other) => Value.CompareTo(other.Value); } 我想知道为什么这样的东西默认不起作用: MyComparable obj1 = new MyComparable(1), obj2 = new MyComparable(2); if (obj1 < obj2) { /*…*/ } 我们知道obj1 < obj2 == true因为我们实现了CompareTo […]

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方法问题调用重载方法

我遇到了有趣的事情(在Java和C#中都一样)。 Java代码: public class TestStuff { public static void main(String[] args) { Printer p = new PrinterImpl(); p.genericPrint(new B()); } } class PrinterImpl implements Printer { void print(A a) { System.out.println(“a”); } void print(B b) { System.out.println(“b”); } @Override public void genericPrint(T b) { print(b); } } interface Printer { public void genericPrint(T a); } […]

C#和Java中的方法重载

我在C#中运行了以下方法。 public float Add(float num1, long num2) { Console.WriteLine(“method 1”); return 0; } public float Add(int num1, float num2) { Console.WriteLine(“method 2”); return 0; } 在这里,如果我调用Add(1,1) ,它会给出歧义。 现在让我在第一个方法中交换float和long位置,如下所示: public float Add(long num1, float num2) { Console.WriteLine(“method 1”); return 0; } public float Add(int num1, float num2) { Console.WriteLine(“method 2”); return 0; } 现在它打印“方法2”作为输出。 第一种情况模棱两可的原因是什么? 如果我在我的代码中编写以下两种方法: […]

Visual Studio的C#intellisense可以给出一个提示,首先显示某个方法过载吗?

我有两种相互重载的方法 public class Car { public int GetPrice(string vinNumber) { string make = Database.GetMake(vinNumber); // expensive operation string model = Database.GetModel(vinNumber); // expensive operation int year = Database.GetYear(vinNumber); // expensive operation return this.GetPrice(make, model, year); } public int GetPrice(string make, string model, int year) { // Calculate value and return } } 在我的示例中,GetPrice(make,model,year)重载执行起来很便宜,但GetPrice(vinNumber)方法很昂贵。 问题是昂贵的方法具有最少的参数并且它首先出现在C#intellisense中。 这两种方法都是有效的,但我想鼓励人们称之为便宜的方法。 […]

通用方法解决方案

请考虑以下代码: public class Tests { public void Test() { Assert.AreEqual(“Int”, DoSomething(1)); } public static string DoSomething(T value) { return “Generic”; } public static string DoSomething(int value) { return “Int”; } } 正如所料,将调用非genericsDoSomething方法。 现在考虑以下修改: public class Tests { public void Test() { Assert.AreEqual(“Int”, DoSomething(1)); } public static string DoSomething(T value) { return “Generic”; } public static […]

方法重载 – 好的还是坏的设计?

我喜欢重载方法来支持越来越多的默认情况。 方法重载对性能的影响是什么? 根据您的经验,是否建议使用重载方法? 限制是多少? 解决方法有哪些?

使用genericsC#重载:错误还是function?

我们有一个以下简化示例: void Foo(IEnumerable collection, params T[] items) { // … } void Foo(C collection, T item) where C : ICollection { // … } void Main() { Foo((IEnumerable)new[] { 1 }, 2); } 编译说: 类型’System.Collections.Generic.IEnumerable’不能在generics类型或方法’UserQuery.Foo(C,T)’中用作类型参数’C’。 没有从’System.Collections.Generic.IEnumerable’到’System.Collections.Generic.ICollection’的隐式引用转换。 如果我将Main更改为: void Main() { Foo((IEnumerable)new[] { 1 }, 2); } 它会工作正常。 为什么编译器没有选择正确的重载?