Each method has a signature. The signature comprises the method's name and its parameters (excepting their names), but not the method's return type. Method Overloading is one of the forms of polymorphism. This is compile time polymorphism because at compile time the compiler able to distinguish between the methods that is declared with same name but different number of arguments.


 


Conditions which should be fulfilled for Method overloading:



  • Method Name should be same for all the methods that are participating in method Overloading.

  • The number of arguments or type of arguments should be different.

  • No class allowed two methods to have same signature.


Note:- Like methods constructors can also be overloaded.


 


Example: Demonstrate Method Overloading


 




 


 


ClsOverLoading19.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


 class ClsOverLoading19


  {


    int a;


    int b;


  //Constructor OverLoading


  public ClsOverLoading19() { a = 10; b = 20; }


  public ClsOverLoading19( int a, int b) { this .a = a; this .b = b;}


  public ClsOverLoading19( ClsOverLoading19 obj) { this .a = obj.a;


  this .b = obj.b; }


  public int result() { return a + b; }


 


  //Method OverLoading


  public string display() { return "single method with no parameter" ; }


  public string display( string strA) { return strA; }


  public string display( string strA, string strB) { return strA + ":: " + strB ; }


  public string display( string strA, int intA) { return strA + " :: " + intA.ToString() ; }


  }


}


 


Form19.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace cSHARPEXAMPLES


{


  public partial class Form19 : Form


   {


  public Form19()


    {


        InitializeComponent();


    }


  private void button1_Click( object sender, EventArgs e)


    { // Call Single Parameter Method


        ClsOverLoading19 obj = new ClsOverLoading19 ();


    MessageBox .Show(obj.display());


    }


  private void button2_Click( object sender, EventArgs e)


    { //Call Parameterized Method


        ClsOverLoading19 obj = new ClsOverLoading19 ();


    MessageBox .Show(obj.display( "vikrant" ));


    MessageBox .Show(obj.display( "Vikrant" , "Dogra" ));


    MessageBox .Show(obj.display( "Hi I am " ,27));


    }


  private void button3_Click( object sender, EventArgs e)


    { //Constructor overloading    


        ClsOverLoading19 obj = new ClsOverL    oading19 ();


        ClsOverLoading19 obj1 = new ClsOverLoading19 (30,40);


        ClsOverLoading19 obj2 = obj1;


    MessageBox .Show( "Default Constructor result " + obj.result());


    MessageBox .Show( "Parameterized Constructor result " + obj1.result());


    MessageBox .Show( "Copy Constructor result " + obj2.result());


    }


  }


}


 


 


Output:


 


Click On: Calling Single Parameter Method




 


Click On: Call Parameterized Method


 


  


 


Click On: Constructor Overloading


 


  


 

                    

Copyright © 2013 VisualBuilder. All rights reserved