Function Overloading in C# .Net

Function Overloading

Function overloading is a concept of writing more than one function  with same name and with different types of arguments.

 

Observations

Proto typepossible to overload
Public void Print ()1 & 2  Not possible
Private Void Print ()1 & 3 Not possible
Public int Print () 1& 4 Yes
Public void Print (Int x) 4 & 5 NO
Public void Print (int y) 4 & 6   Yes 
Public void Print (ref int x)4 & 7  Yes
Public Void Print  (out int x) 6 & 7 No
Public void Print  () 1 &8  Yes [Not overloading concept]

 

Example on function overloading

Open Window forms Application Project

Place a Button

Code in GD

 

Class Test

{

Public void print  ( float x)

{

Message . Box. Show  (“From float :”  + X);

}

Public void Print  (double X)

{

Message . Box. Show  (“From double:”  + X);

}

Public void Print  (decimal X)

{

Message . Box. Show  (“From decimal:”  + X);

}

}//test

 

Code for button1_click

 

Test t = new Test ();

 

  1. Print (4.0) ;//

//t. Print (4); error

  1. Print (4f);//float
  2. Print (4m);//decimal
  3. Print (4d);//double