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

查看:122
本文介绍了具有相同名称和签名,但在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.

有人能告诉我下面的:

  1. 是上面的事情可能在任何情况下(像之一的基类,一个在派生类中ATLEAST?)如果是的话是什么类型呢?像编译或运行时多态性?

  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 的返回类型将不会编译为字符串不是对象

    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; }
    }
    

  • 如果名字是相同的,但参数不同,那么这是方法过载。这是多态性(特设多态性)的一种形式。重载是在编译型静态解析(除非你使用动态在这种情况下,他们被推迟到运行时)。

    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天全站免登陆