C# 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  w  w.  ja  v  a2 s.co  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;/*from   w w  w . j av  a 2 s  .  c  o m*/

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 ww.  j a v  a 2 s  .  c  o m*/
   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  w w  . jav a  2  s  . co 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//from  ww w.ja  va  2  s.  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//from w w w.ja v  a 2 s .co 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; /*from ww w  .  j a v  a2 s . c  o  m*/
 
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.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor