Tag: 方法重载

为什么方法重载在这个C#程序中不起作用?

namespace test { class Program { static void Main(string[] args) { Derived obj = new Derived(); int i = 10; obj.Foo(i); Console.ReadLine(); } } class Base { public virtual void Foo(int i) { Console.WriteLine(“Base:Foo()”); } } class Derived:Base { public override void Foo(int i) { Console.WriteLine(“Foo(int)”); } public void Foo(object i) { Console.WriteLine(“Foo(object)”); } } […]

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”作为输出。 第一种情况模棱两可的原因是什么? 如果我在我的代码中编写以下两种方法: […]

在C#中使用params的成本

有没有人建议在C#中使用params进行方法参数传递。 我正在考虑为前6个参数进行重载,然后使用paramsfunction进行7次重载。 我的理由是避免paramsfunction需要额外的数组分配。 这适用于一些高性能的实用方法。 任何建议? 创建所有重载是浪费代码吗?

方法重载

当我使用TemplateA类型的参数调用EntryPoint时,我总是收到exception,因为总是调用第一个重载。 我期望发生的是由于动态绑定将调用最具体的方法(第二次重载)。 有什么想法吗? private object _obj; public void EntryPoint(object p) { myFunc(p); } //first overload private void myFunc(object container) { throw new NotImplementedException(); } //second overload private void myFunc(TemplateA template) { _obj = new ObjTypeA(template); } //third overload private void myFunc(TemplateB template) { _obj = new ObjTypeB(template); }

.Netinheritance和方法重载

这是一个代码示例: class Program { static void Main(string[] args) { var obj = new DerivedClass(); obj.SomeMethod(5); } } class BaseClass { internal void SomeMethod(int a) { } } class DerivedClass : BaseClass { internal void SomeMethod(long a) { } } 有人可以解释一下为什么派生类调用的方法(而不是基类方法)? 我需要详细解释这种情况。 我将非常感谢任何有用文章的链接。 谢谢。

方法解析顺序

假设我们有: public class FooBase { public void Write(byte value) { //something } public void Write(int value) { //something } } public class Foo : FooBase { public void Write(decimal value) { //something } } 比这个: var writer = new Foo(); writer.Write(5); //calls Write(decimal) !! writer.Write((byte)6); //calls Write(decimal) !! 将调用Write(十进制)重载。 为什么? 我怎么能调用Write(int)或Write(byte)?

使用值和引用参数类型重载的方法

我有以下代码: class Calculator { public int Sum(int x, int y) { return x + y; } public int Sum(out int x, out int y) { x = y = 10; return x + y; } } class Program { static void Main(string[] args) { int x = 10, y = 20; Calculator calculator = new […]

重载方法选择逻辑

鉴于以下重载方法: public string Test(long item) { return “Test with a long was called!”; } public string Test(int item) { return “Test with an int was called!”; } public string Test(object item) { return “Test with an object was called!”; } 当我调用Test() ,传递一个short ,如下所示: short shortValue = 7; var result = Test(shortValue); 为什么值result等于”Test with an int […]

方法重载的通用约束

我有一个带有一些generics方法的接口,我想实现一个带有重载的方法来接受一个类的实例,或者它的PK值(它是一个int或GUID但确实变化)。 我添加了类似这些示例的方法: void DoSomething(TKey key) where TKey: struct; void DoSomething(TModel model) where TModel : class; 其中第二个上的’DoSomething’方法名称突出显示,错误是 类型’ISomeStuff’已经定义了一个名为’DoSomething’的成员,它具有相同的参数类型。 我对此感到惊讶,因为我已经通过参数明确定义了不同的类型:一个是类,另一个是结构。 为什么这不足以使签名不同?

C#中方法重载的不同行为

我正在浏览C#Brainteasers( http://www.yoda.arachsys.com/csharp/teasers.html )并遇到一个问题:这段代码的输出应该是什么? class Base { public virtual void Foo(int x) { Console.WriteLine (“Base.Foo(int)”); } } class Derived : Base { public override void Foo(int x) { Console.WriteLine (“Derived.Foo(int)”); } public void Foo(object o) { Console.WriteLine (“Derived.Foo(object)”); } } class Test { static void Main() { Derived d = new Derived(); int i = 10; […]