C# method overloading
In this chapter you will learn:
- What is method overloading
- How to create overloaded methods
- Note for Pass-by-value versus pass-by-reference
- Example for method overload
- What are the easy errors in method overloading
- return type is not part of the signature
- ref and out modifiers and method overloading
- ref and out cannot coexist
- How is the type conversion affecting the method overloading
Description
We can overload methods by having multiple methods with the same name, as long as the signatures are different.
Syntax
For example, the following methods can all coexist in the same type:
void Foo (int x);
void Foo (double x);
void Foo (int x, float y);
void Foo (float x, int y);
However, the following pairs of methods cannot coexist in the same type, since the return type and the params modifier are not part of a method's signature:
void Foo (int x);
float Foo (int x); // Compile-time error
void Goo (int[] x);
void Goo (params int[] x); // Compile-time error
/* w ww.j av a2 s.c o m*/
Note
Whether a parameter is pass-by-value or pass-by-reference is also part of the signature. For example, Foo(int) can coexist with either Foo(ref int) or Foo(out int). However, Foo(ref int) and Foo(out int) cannot coexist:
void Foo (int x);
void Foo (ref int x); // OK so far
void Foo (out int x); // Compile-time error
Example
The following code defines a class, Math. Math has two methods with the same name. It is legal since the signatures are different.
using System;//w w w . j a v a 2 s. com
class Math
{
public static int add(int i, int j)
{
return i + j;
}
public static float add(float f, float f2)
{
return f + f2;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Math.add(1, 2));
Console.WriteLine(Math.add(1.1F, 2.2F));
}
}
The code above generates the following result.
Example 2
params is not part of the signature.
class Math{//from w w w. j av a2 s. com
static void sum(int[] anArray){
}
static void sum(params int[] anArray){
}
}
Compile-time error
Example 3
The return type is not part of the signature.
class Math{// w ww.j a v a2 s .c o m
int add(int i, int j){
return i+j;
}
float add(int i, int j){
return (float)(i+j);
}
}
Compile-time error.
Example 4
ref and out modifiers are part of the method signature.
class Math/* www .j a va 2s . c om*/
{
int add(int i, int j)
{
return i + j;
}
int add(ref int i, ref int j){
return i + j;
}
}
Example 5
The following code shows that ref and out cannot coexist.
class Math// w w w. j a va 2s .c o m
{
int add(out int i, out int j)
{
return i + j;
}
int add(ref int i, ref int j){
return i + j;
}
}
Compile time error:
Example 6
The following code shows automatic type conversions can affect overloaded method resolution.
using System; // w w w . j a v a 2s. c om
class Overload2 {
public void f(int x) {
Console.WriteLine("Inside f(int): " + x);
}
public void f(double x) {
Console.WriteLine("Inside f(double): " + x);
}
}
class MainClass {
public static void Main() {
Overload2 ob = new Overload2();
int i = 10;
double d = 10.1;
byte b = 99;
short s = 10;
float f = 11.5F;
ob.f(i); // calls ob.f(int)
ob.f(d); // calls ob.f(double)
ob.f(b); // calls ob.f(int) -- type conversion
ob.f(s); // calls ob.f(int) -- type conversion
ob.f(f); // calls ob.f(double) -- type conversion
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is a recursive methods
- How to write recursive methods
- Display a string in reverse by using recursion
- How to calculate fibonacci value in C#