Information Technology Reference
In-Depth Information
Method Overloading
A class can have more than one method with the same name. This is called method overload-
ing . Each method with the same name must have a different signature than the others.
￿
The signature of a method consists of the following information from the method
header of the method declaration:
- The name of the method
- The number of parameters
- The data types and order of the parameters
-The parameter modifiers
￿
The return type is not part of the signature—although it is a common mistake to believe
that it is.
￿The names of the formal parameters are also not part of the signature.
Not part of signature
long AddValues( int a, out int b) { ... }
Signature
For example, the following four methods are overloads of the method name AddValues .
class A
{
long AddValues( int a, int b) { return a + b; }
long AddValues( int a, int b, int c) { return a + b + c; }
long AddValues( float a, float b) { return a + b; }
long AddValues( long a, long b) { return a + b; }
}
The following code shows an illegal attempt at overloading the method name AddValues .
The two methods differ on the return types and the names of the formal parameters. But they
still have the same signature, because they have the same method name, and the number,
types, and order of their parameters are the same. The compiler would produce an error mes-
sage for this code.
class B Signature
{
long AddValues( long a, long b) { return a+b; }
int AddValues( long c, long d) { return a+b; }
}
Signature
Search WWH ::




Custom Search