Thumb

What is method overloading?

1/8/2020 6:35:02 AM

Method overloading look like constructor overloading but it’s works with method. based on change parameter method behaver change it’s called method overloading. Method name and signature are same but different parameter it’s called method overloading. This is the common way of implementing polymorphism. Now given bellow the code and explain the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using testForClass1;
namespace testFor
{
    public class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            int firstMethod = student.Add(10);
            int secondMethod = student.Add(10, 20);
            double thirdMethod = student.Add(10, 20, 5.6);
            Console.WriteLine("First Method Value : " + firstMethod);
            Console.WriteLine("Second Method Value : " + secondMethod);
            Console.WriteLine("Third Method Value : " + thirdMethod);
            Console.Read();
        }
    }
    public class Student
    {
        public int Add(int a)
        {
            return a;
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
        public double Add(int a, int b, double c)
        {
            return a + b + c;
        }

    }
}

In this Student class we can see the three add method but deferent parameter Then method name is same it’s called method overloading. In the Main method we create the object of the class and pass the parameter then receive the value and print.