Home > DeveloperSection > Beginner > Method Overloading in C#

Method Overloading in C#


C# C#  OOPS 
Ratings:
0 Comment(s)
 508  View(s)
Rate this:

Method Overloading in C#


Method overloading is a feature which allows a class to have two or more methods having same name but different signature means (multiple behavior but different signature). The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.

Most of the programming languages supports a technique called default/optional parameters. It allows to programmer to make one or several parameter optional, by giving them a default value.

 

For example: Create a class ‘MethodOverloading'. Here notice one thing, method are same but signature are different.


Source Code:

MethodOverloading class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OopsConcepts
{
    class MethodOverLoading
    { 
        public int Addition(int a, int b)
        {
            int x;
            return x = a + b;
        }
        public int Addition(int a, int b, int c)
        {
            int y;
            return y = a + b + c;                        
        }
        public float Addition(float a, float b)
        {
            float u;
            return u = a + b;
        }
        public float Addition(float a, float b, float c)
        {
            float v;
            return v = a + b + c;
        }
    }
}


OverloadingTest class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OopsConcepts
{
    class OverLoadingTest                                             
    {
        static void Main(string[]args)
        { 
          // Method Overloading Test
            MethodOverLoading mol = new MethodOverLoading();
            Console.WriteLine("Addition of {0}", mol.Addition(25, 25));
            Console.WriteLine("Addition of {0}", mol.Addition(2.5f, 2.5f));
            Console.ReadLine();
     }
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick