Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...