method overloading in c#

Method Overloading in C#

Method Overloading in C#

What is Method Overloading in C# ?

Method overloading in c# is also known as Function overloading in C#. Method overloading allows a class to declare multiple methods with same name,but different signature.

 

//Method Overloading also known as Function overloading
//Method overloading is same method name with different signatures
//different signature means method must either of below possibility 
//     1. different no of parameters 
//     2. different datatype of parameter 
//     3. different sequence of parameters


//Ex:1- Working properly
public int sum(int a, int b) { return a + b; }
public int sum(int a, int b, int c) { return a + b + c; }

//Ex:2- doen't work properly as same method name with same signature
public int sum1(int a, int b) { return a + b; }
//public int sum1(int b, int a) { return a + b; }

//Ex:3- Working properly as parameter sequence is different though method name is same
public void method(int a, string b) { }
public void method(string b, int a) { }

//Ex:4- Working properly as parameter signature is different by out keyword
// out keyword is a reference of that "string a" parameter so it's different from normal "string a"
public void methodWithOut(string a, int b) { }
public void methodWithOut(out string a, out int b) { a = ""; b = 10; }

//Ex:5- Working properly as parameter signature is different by ref keyword
//ref keyword is a reference of that "string a" parameter so it's different from normal "string a"
public void methodWithRef(string a, int b) { }
public void methodWithRef(ref string a, ref int b) { }

//Ex:6- doen't work properly as same method name with same signature 
//with reference type using ref and out
public void methodWithRefOut(ref string a,ref int b) { }
public void methodWithRefOut(out string a, out int b) { a = ""; b = 10; }

 

Leave a Reply