Hi,
can somebody give me a good example of method overloading in c#, and overloaded functions in c#, or even a pointers to a good language reference site for VS.net and all concernned languages?
cheers all
Kai :wave:
Printable View
Hi,
can somebody give me a good example of method overloading in c#, and overloaded functions in c#, or even a pointers to a good language reference site for VS.net and all concernned languages?
cheers all
Kai :wave:
Method Overloading:
VB Code:
public class addNumbers { public int Add(int a, int b) { return a+b; } public int Add(int a, int b, int c) { return a+b+c; } }
Overloading is providing multiple implementations of a named method, where method signature is different for each one. Hence, the compiler determines which method to call based on the number and type arguments you pass.
To expand on fret's example:
The signature for this method isCode:public int Add(int a, int b)
whereas the signature for this methodCode:int(int, int)
isCode:public int Add(int a, int b, int c)
As the signature has to differ you cannot overload a method by merely providing arguments with different names as only the types are considered in the signature.Code:int(int, int, int)
Hence, you can do this
Code:public int Add(int a, int b)
{
return a + b;
}
public float Add(float a, float b)
{
return a + b;
}