Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function overload

Tags:

c#

Can I have two same function name with same parameters but different meaning.

For example:

public void test(string name) 

public void test(string age) 

Thank you.

like image 258
Alvin Avatar asked Sep 14 '12 09:09

Alvin


1 Answers

No, you can't. The signature is not different - it doesn't matter what the parameter names are.

Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.

http://msdn.microsoft.com/en-us/library/ms173114.aspx

Like a few other answers have stated, consider the type of data you're taking in. Name is indeed a typical string, but does age have to be? If you allow it to be a - for example - int then you can overload your method as you wish.

like image 62
J. Steen Avatar answered Sep 23 '22 15:09

J. Steen