C#中具有相同名称和签名但返回类型不同的方法 [英] Method with same name and signature but different return type in C#

查看:444
本文介绍了C#中具有相同名称和签名但返回类型不同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受采访时被问到以下问题:

I had an interview where I was asked the following:

问题:具有相同名称和签名但返回类型不同的方法.有可能吗,他问我叫什么类型.

Question: A method with same name and signature but different return type. Is it possible and what is this type called he asked me.

有人可以告诉我以下内容吗?

Can someone please tell me the following:

  1. 在任何情况下(在基类中至少有一个,在派生类中至少有一个?),上述情况是否可能?像编译或运行时多态吗?

  1. Is above thing possible in any scenarios (Like one in base class and one in derived class atleast ?) If so what type is it ? Like compile or runtime polymorphism ?

在编译时多态性中,如果方法的返回类型和签名也不同,该怎么办?但是只有函数的名称是相同的.仍然是编译时多态吗?

In compile time polymorphism, what if the return types of methods are also different along with signature ? But only the name of function is same. Is it compile time polymorphism still ?

在重写中,如果我具有不同的返回类型,但方法名称和签名相同,该怎么办?是否有可能 ? (他问我这个问题,我回答错了:()请帮助我.

In overriding, what if I have different return type but the method name and signature are same ? Is it possible ? (He asked me this question, I answered wronglY :() Please help me.

谢谢

推荐答案

  • 我认为问题是关于返回类型协方差

    它允许一种方法返回比基本类型中声明的类型更衍生的类型.

    It allows a method to return a more-derived type than the one declared in a base type e.g.

    public interface ISomeInterface
    {
        object GetValue();
    }
    
    public class SomeClass : ISomeInterface
    {
        public string GetValue() { return "Hello world"; }
    }
    

    这在Java中受支持,但在C#中不受支持.由于SomeClass.GetValue的返回类型是string而不是object,因此上述内容将不会编译.

    This is supported in Java but not in C#. The above will not compile since the return type of SomeClass.GetValue is string not object.

    请注意,您不能重载仅基于返回类型的方法,即以下无效:

    Note that you cannot overload methods based on return-type alone i.e. the following is not valid:

    public class SomeClass
    {
        public int GetValue() { return 1; }
        public string GetValue() { return "abc"; }
    }
    

    您可以使用接口执行类似的操作,尽管您需要明确实现它们以消除歧义:

    You could do something similar using interfaces although you would need to implement them explicitly to disambiguate:

    public interface IValue<T> { T GetValue(); }
    public class SomeClass : IValue<int>, IValue<string>
    {
        string IValue<string>.GetValue() { return "abc"; }
        int IValue<int>.GetValue() { return 1; }
    }
    

  • 如果名称相同但参数不同,则这是方法重载.这是一种多态性(即席多态性).重载以编译类型静态解决(除非您使用dynamic,在这种情况下,它们会推迟到运行时).

    If the names are the same but the parameters are different then this is method overloading. This is a form of polymorphism (ad-hoc polymorphism). Overloads are resolved statically at compile-type (unless you're using dynamic in which case they are deferred to run-time).

    您可以重载参数的数量及其类型,因此以下所有内容均有效:

    You can overload on both the number of parameters and their type, so the following are all valid:

    public void DoSomething(int value) { }
    public void DoSomething(string value) { }
    public void DoSomething(int value, string value) { }
    

    请注意,您可以更改这些方法的返回类型-方法不仅不能仅根据其返回类型进行重载,而且如果它们的参数列表不同,它们也可以不同.

  • Note that you can vary the return type of these methods - methods cannot only be overloaded based on their return type alone but they can differ if their parameter lists are different.

  • 同样是返回类型协方差,C#不支持.

    Again this is return type covariance and is not supported in C#.

  • 这篇关于C#中具有相同名称和签名但返回类型不同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆