Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method with same name and signature but different return type in 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. 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 ?

  2. 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 ?

  3. 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.

Thank you

like image 399
Jasmine Avatar asked Mar 12 '13 13:03

Jasmine


People also ask

Can two methods have the same name but different return type?

We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types.

Can you have two methods in a class with the same method signature but different return types yes?

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

What happened if two methods have same name same parameters but different return types?

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.

Is it possible to have two methods in a class with same method signature but different?

No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4. 2) methods with same signature is not allowed in this case.


3 Answers

  • I assume the question is about return type covariance

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

    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.

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

  • like image 115
    Lee Avatar answered Oct 07 '22 17:10

    Lee


    In C#, you cannot have methods like

    int Foo() { return 1; }

    void Foo() { return; }

    They must vary by more than return type.

    If the arguments are different, then you're good to go.

    int Foo(string x) { return 1; }

    void Foo(double x) { return; }

    like image 26
    Todd Sprang Avatar answered Oct 07 '22 16:10

    Todd Sprang


    Though return type covariance is not supported in C#, it is possible to emulate it by using explicit implementation and method hiding. That is a pattern which is used thoroughly in the ADO.NET APIs.

    E.g.:

    public interface ISomeValue { }
    public abstract class SomeValueBase : ISomeValue { }
    public class SomeValueImpl : SomeValueBase { }
    
    public interface ISomeObject { ISomeValue GetValue(); }
    
    public abstract class SomeObjectBase : ISomeObject
    {
        ISomeValue ISomeObject.GetValue() { return GetValue(); }
        public SomeValueBase GetValue() { return GetValueImpl(); }
    
        protected abstract SomeValueBase GetValueImpl();
    }
    
    public class SomeObjectImpl : SomeObjectBase
    {
        protected override SomeValueBase GetValueImpl() { return GetValue(); }
        public new SomeValueImpl GetValue() { return null; }
    }
    

    By doing that, the net result of calling GetValue() is that it will always match the most specific available type.

    like image 39
    Jean Hominal Avatar answered Oct 07 '22 16:10

    Jean Hominal