Method with same name and signature but different return type in C#

后端 未结 9 1767
南旧
南旧 2020-12-08 01:01

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 c

相关标签:
9条回答
  • 2020-12-08 01:26

    Since this is an interview question, you can run with your answer a bit.

    Strictly speaking, a method with different return type and the same signature is not possible. However, broadly speaking, there are many ways of implementing a method whose concrete run time return type varies.

    One is using generic parameters. Another is returning an interface or a super class with multiple implementations. Or, you can return an object, which can be cast to anything.

    As many mentioned, you can also use the "new" keyword to return a derived type of the same method in a subclass.

    0 讨论(0)
  • 2020-12-08 01:26

    yes you can use the same method, same parameters (need to customize) and different return values.

    Just follow the codes below, it might help you.

    public class myClass
    {
        public int myReturn() { return 123; }
        public string myReturn(string temp = null) { return "test"; }
    }
    

    the thing is, it requires parameter to make execute the functions, but you still able to ignore the parameters since you have string temp = null as optional parameters, which is you still call the functions with or without the parameters.

    0 讨论(0)
  • 2020-12-08 01:27

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

0 讨论(0)
  • 2020-12-08 01:29

    I recently had this exact issue since I was parsing a config file with various types that all use a standard sort of config.Parse(string settingName) type interface.

    1st Solution Generic Method:

    T Parse<T> (string settingName)
    {
        T Result;
        doParsing...
        return T;
    }
    

    I didn't really like this since it involved explicitly specifying the type that's being used such as someSetting = Parse<float>("param");

    So the solution I used is redundant but much more clean in my opinion:

    T Parse<T> (string settingName, out T result)
    {
        doParsing...
        return T;
    }
    

    The out variable and the return are identical so it's a bit redundant but it enables what I think is a much cleaner interface:

    setting = Parse("intparam", out setting);
    

    And you get methods that vary only by return type for a slight cost of redundancy. On top of that if the type of your data changes, for instance from a double to a float, then you everything will continue working just fine, whereas with the first solution you'd end up getting cannot implicitly convert errors.

    0 讨论(0)
  • 2020-12-08 01:32

    You can use a dynamic return type:

    • dynamic (C# Reference)
    • Using Type dynamic (C# Programming Guide)
    0 讨论(0)
  • 2020-12-08 01:39

    Yes, it is possible to have multiple methods with the same signature but different return types, using Explicit Interface Implementation as shown here:

    public interface I {
      int foo();
    }
    
    public class C : I {
      double foo() { return 2.0; }
      int I.foo() { return 4; }
    }
    
    0 讨论(0)
  • 提交回复
    热议问题