top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Method Overloading in C#.NET

0 votes
255 views

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.


In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”. 


But in C# no need to use any keyword while overloading a method either in same class or in derived class. 


While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument.


Example for Method Overloading
 

using System;

namespace ProgramCall
{

    class Class1
    {

        public int Sum(int A, int B)
        {
            return A + B;
        }

        public float Sum(int A, float B)
        {
            return A + B;
        }
    }

    class Class2 : Class1
    {
        public int Sum(int A, int B, int C)
        {
            return A + B + C;

        }
    }

    class MainClass
    {
        static void Main()
        {

            Class2 obj = new Class2();

            Console.WriteLine(obj.Sum(10, 20));

            Console.WriteLine(obj.Sum(10, 15.70f));

            Console.WriteLine(obj.Sum(10, 20, 30));

            Console.Read();
        }

    }
}


Output

30
25.7
60


Note

Method overloading provides more than one form for a method. Hence it is an example for polymorphism. 


In case of method overloading, compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation it self. Hence method overloading is an example for compile time polymorphism.

posted Feb 9, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Creating a method in derived class with same signature as a method in base class is called as method overriding.

Same signature means methods must have same name, same number of arguments and same type of arguments.

Method overriding is possible only in derived classes, but not within the same class. 


When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.


To allow the derived class to override a method of the base class, C# provides two options, virtual methods and abstract methods.


Examples for Method Overriding in C#

 

using System;

namespace methodoverriding
{
    class BaseClass
    {
        public virtual  string YourCity()
        {
            return "New York";
        }
    }

    class DerivedClass : BaseClass
    {
        public override string YourCity()
        {
            return "London";
        }
    }

    class Program
    {
        
        static void Main(string[] args)
        {
            DerivedClass obj = new DerivedClass();
            string city = obj.YourCity();
            Console.WriteLine(city);
            Console.Read();
        }
    }
}
 

Output

London


Example - 2 implementing abstract method
 

using System;

namespace methodoverridingexample
{
    abstract class BaseClass
    {
        public abstract  string YourCity();
        
    }

    class DerivedClass : BaseClass
    {
        public override string YourCity()   //It is mandatory to implement absract method
        {
            return "London";
        }

        private int sum(int a, int b)
        {
            return a + b;
        }
    }

    class Program
    {
        
        static void Main(string[] args)
        {
            DerivedClass obj = new DerivedClass();
            string city = obj.YourCity();
            Console.WriteLine(city);
            Console.Read();
        }
    }
}


Output

London

READ MORE
...