Tag: method overloading

为什么C#允许通过可选参数进行模糊函数调用?

我今天遇到了这个,我很惊讶我之前没有注意到它。 给出一个简单的C#程序,类似于以下内容: public class Program { public static void Main(string[] args) { Method(); // Called the method with no arguments. Method(“a string”); // Called the method with a string. Console.ReadLine(); } public static void Method() { Console.WriteLine(“Called the method with no arguments.”); } public static void Method(string aString = “a string”) { Console.WriteLine(“Called the method […]

C#可选参数或方法重载?

由于C#添加了可选参数,因此使用可选参数或方法重载被认为是更好的做法,或者是否存在您希望使用其中一个的特定情况。 即具有许多参数的函数更适合w /可选参数?

当派生类型为1作为参数传递时,双向隐式可转换类型的重载之间的模糊调用

(试图找到一个总结问题的标题可能是一项非常艰巨的任务!) 我有以下类与一些重载方法,产生一个调用歧义编译器错误: public class MyClass { public static void OverloadedMethod(MyClass l) { } public static void OverloadedMethod(MyCastableClass l) { } //Try commenting this out separately from the next implicit operator. //Comment out the resulting offending casts in Test() as well. public static implicit operator MyCastableClass(MyClass l) { return new MyCastableClass(); } //Try commenting this out […]