Method Overloading in C#

Method overloading is an example for compile time polymorphism. Method overloading is ability to have more than one function of same name in the same scope. In other word, we can have two or more methods with the same name but different signatures.In case of method overloading, compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation. So it's required to achieve method overloading, Each method signature must be unique within the type. Members can have the same name as long as their parameter lists differ.

There is one more thing to note that changing the return type of a method does not make the method unique.

Now question is why  we use method Overloading.

So the answer is it's used or recommended when multiple methods have the same purpose, but there is more than one way to start it.

We can achive method overloading by the three way :-

1. Number of parameters

2. Type of parameters.

3. Order of parameters.

Let's start  with a couple of really simple code for above :-

 class Program
    {
        static void Main(string[] args)
        {
            Overloading obj = new Overloading();
            int r1 = obj.DoSomeThing(2);
            int r2 = obj.DoSomeThing(2, 4);
            string r3 = obj.DoSomeThing(2, "str");
            string r4 = obj.DoSomeThing("str", 4);
            Console.WriteLine("r1={0}, r2={1}, r3={2}, r4={3}",r1,r2,r3,r4);
            Console.Read();
        }
    }
 
    public class Overloading
    {
 
        public int DoSomeThing(int x) // Method with One parameters
        {
            return x;
        }
        public int DoSomeThing(int x, int y) // Method with Same Name(No of parameters is Differ)
        {
            return x + y;
        }
 
        //
        public string DoSomeThing(int x, string y) // Method with Same Name(Type of parameters is Differ)
        {
            return x.ToString() + y;
        }
 
        public string DoSomeThing(string x, int y) // Method with Same Name(Order of parameters is Differ)
        {
            return x + y.ToString();
        }
 
 
    }

Here, I define DoSomeThing method in four different ways. The first one takes one parameter while the second method takes two integer type parameters having different number of parameters. In the third one, there is two parameters but have different type of parameter and fourth one have a different order of parameters. Then I called all four overload method by their suited parameters Lists.

Thanks for reading.

;



share post :

Popular Posts

How to Create WCF service and host in Windows Service in WCF Service / Jan 13, 2017
Dependency properties in WPF in WPF / Oct 01, 2018
Static constructor in CSharp(C#.Net) in C#.Net / Feb 11, 2017
How to use tuples with more new feature in C# 7.0 in C#.Net / Jan 20, 2017
Method Overloading in C# in C#.Net / Feb 07, 2017
Interface in c# with example in C#.Net / May 14, 2017

Say Hi It’s Free!

  • Ghaziabad Uttar Pradesh India

  • avnish@ymail.com

  • (+91)-847-107-5432

Success/Error Message Goes Here

Avnish Kumar

.Net Developer

Download Resume
Drink A Coffee With Me Today