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),
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)
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).
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:-
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:-
click below for download whole application.
DOWNLOAD
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:-
- Abstract Class and Abstract Method
- Structure and Class
- delegate in c#
- params keyword in c#
- Exception handling
To Get the Latest Free Updates Subscribe
click below for download whole application.
DOWNLOAD
4.Return type is different.
ReplyDeleteEX:-
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 .
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