Tuesday 10 November 2015

How to use Method overloading in c# .Net?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.                    string ... thumbnail 1 summary

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. 

                  string sum(int A)
                  string sum(int A, int B)

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

using System;
namespace MethodOverloading
{
    class Class1
    {
        public int Sum(int A, int B)
        {
            return A + B;
        }
        public float Sum(int A, float B)
        {
            return A + B;
        }
    }
    class MainClass
    {
        static void Main()
        {
            Class1 obj = new Class1();
            Console.WriteLine(obj.Sum(20, 30));
            Console.WriteLine(obj.Sum(20, 15.70f));
            Console.Read();
        }
    }
}

Output :-
50
35.7

2 comments

  1. Techno Softwares having several years experience working with global customers, connecting our professionals to their business needs, as their IT Development & Support Partner. Techno Softwares having a team of dedicated and experienced softwares developers that works for your all business needs. Techno Softwares deals in web design and development, Customized ERPs, CRMs, Web & Mobile Applications, eCommerce platforms etc.

    ReplyDelete
  2. BestDotNetTraining is an MS.NET portal providing high end quality online training courses. We have all topics of MS.NET like C#, ASP.NET, MVC, WPF, WCF etc covered in-depth. Our teaching methodology includes in-depth theory on every topic which prepares you for interview and followed by practical demonstration of the subject so that you can find doing project very easy. Our online training course are designed in such a way that any beginner can start and eventually grow as advanced developer. Dot Net Training in Chennai
    Microsoft Dot Net Training in Chennai

    ReplyDelete