is it possible in C# polymorphism that method will have different return type? I know polymorphism have different parameters but not sure about this
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)
{ }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With