Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading by return type? [duplicate]

public class test
{
    public void xov() { return; }
    public string xov() { return null; }
    public int xov() { return 0; } 
}

public class p
{
    public p()
    {
        test v = new test();
        v.xov();// Here i cann't use defined overloads :(  
    }   
} 

I can overload methods by parameters , but why cann't I overload methods by return type?

like image 989
Real Programmer Avatar asked Apr 13 '26 23:04

Real Programmer


2 Answers

The return type is not considered part of a method's signature. That is, methods cannot be overloaded if they differ only by return type.

like image 199
Kas Avatar answered Apr 16 '26 14:04

Kas


Calling

int x =  Add(5,6);

might be diffrent than:

string y = Add(5,6);

But what if no one reads the return type?

Add(5,6);

How is the compiler suppose to know which add was called? So this option is not legal.

like image 34
omer schleifer Avatar answered Apr 16 '26 15:04

omer schleifer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!