Method Overloading in C#

By
We can  used more than one Method with same name,within same class but with different parameter,that is called method overloading. When the basic functionality of more than one method is same but they are used different parameter,then we can implement method overloading.
Method overloading will be possible if method name is same but there signature is different.

Signature will be different if:-
   1. Number of parameter in method is different.
EX:- 
 show(int x), show(int x,int y),
here both are same method but number of parameter  is different.
    
    2. Data type of parameter is different.
EX:- 
display(int x, int y), display(int x, float y)
Here both method is same but there parameter is different.

    3. Sequence of data type of parameter is different.
EX:- 
Display(int x, string y, float z), Display(int x, float y ,string z).
Here both method is same  but sequence of data type parameter is change.
EXAMPLE:-

using System;
namespace methodoverloading
{
    class Program
    {
        static void Main(string[] args)
        {
            cls obj = new cls();
            obj.display();          //call first display method which takes no 
            obj.display("neha");  //call second display method which takes one 

            Console.ReadLine();
        }
        public class cls
        {
            public void display()
            {

                Console.WriteLine("HELLO");
            }
            public void display(string sname)
            {
                Console.WriteLine("HELLO" + sname);

            }
        }
    }
}
Description:- here class cls have two display method. one takes no parameter and other takes one parameter. when we create object of cls class then all method of cls class will get in memory.
There are some steps to run the program.
Steps1  open the visual studio and go File-> New Project->select console application.
See it:-


Step2:- Click OK-> copy the above program code in Program.cs.
See it:-

 Step4:- Run the application(press F5)->OUTPUT.
See it:-

I hope this helpful for you.
For More:-

  1. Abstract Class and Abstract Method
  2. Structure and Class
  3. delegate in c#
  4. params keyword in c#
  5. Exception handling 
To Get the Latest  Free Updates Subscribe

click below for download whole application.
              DOWNLOAD

2 comments:

  1. 4.Return type is different.
    EX:-
    Public int display(), public void display().
    here both method is same but their return type is different.

    >>When we create object of the class then how to call display() method .

    ReplyDelete
  2. thanks @lalit prajapati,to inform me error on this page. it will give error,because both method is same but return type is different. we can not put return type in under signature.please inform me if any other error on another page.

    ReplyDelete

Powered by Blogger.