Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In OverLoading concept,Why they are not consider return value and why they are consider only parameters in method? For ex: public int Add(int a,int b){...} public String Add(int a,int b){...} . how compiler will find which function called?
Posted

That is exactly why the return type is not used to discriminate between overlaods - you will get a compiler error if you try.

Think about it - assume you have two overloaded methods:
public HSObject Add(int a, int b) { ... }
public HSSlide Add(int a, int b) { ... }
It looks pretty easy to decide which of these should be called, doesn't it?
HSObject obj = Add(1, 2);
HSSlide slide = Add(1, 2);
But what if you did this:
Add(1,2);
Perfectly legal, you are just ignoring the result. But which result are you ignoring?
And if HSSlide inherits from HSObject, which method should I call? I can put either result into an HSObject variable.

The rule is simple: parameters only. Eliminates all possible confusion, for the compiler, and for us as developers!
 
Share this answer
 
Comments
Manas Bhardwaj 11-May-11 12:14pm    
sweet and simple explanation. +5
Sergey Alexandrovich Kryukov 11-May-11 12:19pm    
Hi Manas, please see an important addition in my answer (and my comment below)
--SA
Sergey Alexandrovich Kryukov 11-May-11 12:19pm    
Good explanation, my 5.
Only one important case is missing: the case when ambiguous situation is only detected by looking at the calling code -- very important to know.

It took me some time to build such example -- please see my answer.
--SA
Espen Harlinn 11-May-11 13:00pm    
Nice reply, my 5
It has nothing to do with OOP!

Your example will not compile:

C#
int Add(int a, int b) { return a + b; }
string Add(int a, int b) { return (a + b).ToString(); }


Error message:
Error	1	Type 'ContraOrCovariance.Program' already defines a member called 'Add' with the same parameter types…


All correct.

Overloading works if the compiler has a way to determine statically which function should be compiled in what case. It can only be finally determined by the calling code. Your case is special: even before looking at the calling code the compiler denies to work with so close signatures: parameter list should be different for two or more methods of the same name.

Consider this:
C#
class Base { /*...*/ }
class Derived : Base { /*...*/ }

//...

static void Work(Derived i1, Base i2) { /*...*/ }
static void Work(Base i1, Derived i2) { /*...*/ }

Derived d = new Derived();
Derived b = new Derived();

//...

Work((Base)d, b);
Work(d, (Base)b);

//will not compile: The call is ambiguous between the following methods... [two Work methods]
Work(d, b);


This is the case when ambiguous situation is only detected by looking at the calling code.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 11-May-11 13:01pm    
Good reply, my 5
Sergey Alexandrovich Kryukov 11-May-11 13:18pm    
Thank you, Espen.
--SA
The example you provided is not valid. In order for function overloading to be valid, it needs to have different signature. If the signatures are the same but the return type is different ( as in your case ) then it is not valid. See an example here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900