Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we have different return type of method in c# polymorphism

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

like image 508
saagar Avatar asked Oct 30 '14 17:10

saagar


1 Answers

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

Overloaded methods cannot differ only by the return type.

With polymorphism all parameters and the 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 have to keep the same signature:

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

public class B : A
{
    public override void MyMethod(string arg)
    { }
}
like image 76
BradleyDotNET Avatar answered Sep 22 '22 18:09

BradleyDotNET