Method Overloading in C#

Hi Programmers,welcome to new article of c#.net. This article i’ll write the program for method overloading in c# console application. Method overloading provide facility of multiple methods in a class with same name but different parameters and types. We can do method method overloading by following ways.
1.By altering the number of parameters used.
2.By altering the order of parameters.
3.By giving different data types for the parameters.
Let’s see the codes.

Directly Test below codes into Editor.

using System;
namespace ConsoleApp1 
{
    class Program 
    {
        public void sum()
        {
            Console.WriteLine("Method Without Parameter : ");
        }
        public void sum(int p)
        {
            Console.WriteLine("The value of p = " + p);
        }
        public int sum(int p,int q)
        {
            return p + q;
        }
        public void sum(float f1, float f2)
        {
            Console.WriteLine("Sum = " + (f1 + f2));
        }
        public void sum(int p, float f2)
        {
            Console.WriteLine("Sum = " + (p + (int)f2));
        }
        public static int sum(int p, int q,int r)
        {
            return p + q + r; 
        }
        static void Main(string[] args)   
        {
            Program p = new Program();
            p.sum(); p.sum(12);
            Console.WriteLine("Sum = " + p.sum(8, 16));
            p.sum(7.3f, 11.8f);
            p.sum(8, 66.7f);
            int s = Program.sum(8, 6, 8);
            Console.WriteLine("Sum = " + s);
        }
    }
}

Happy Coding…Thanks.

Post Author: adama