We can have a different return type of method in C # polymorphism

is it possible in C # polymorphism that a method will have a different return type? I know polymorphism has different parameters, but not sure about this

+3


source to share


1 answer


What you are talking about is method overloading; not polymorphism. And you can; with one caveat:

Overloaded methods cannot differ only in return type.

With polymorphism, all parameters and return type must match.

Method overloading occurs when you give two methods the same name:



public void MyMethod(string arg) { }
public int MyMethod(string arg, int arg2) { return 0; }

      

Polymorphism occurs when you override a base class function, but you must keep the same signature:

public class A
{
    public void MyMethod(string arg)
    { }
}

public class B : A
{
    public override void MyMethod(string arg)
    { }
}

      

+5


source







All Articles