Method Overloading in C#

Filed in ASP.NETTags: , , , ,

In object oriented programming languages the term overloading described as single use multiple interface. When we create more than one method with same name then it is called method overloading. To make difference in the method we can use different type of parameters or we can change the order of the same type of parameter or we can use different numbers of parameters in method overloading.

 

There are the following concept that we can use differentiate the methods:
1. Different parameters,
2. Different data type of parameters
3. Same parameter type but order must be different

The method also can be overloaded in derived class. In C# we need not to use any keyword while overloading of methods.
Now we will see how we can overload a method in C# with example.

 

using System;
namespace MOverloading
 {
           class Class1
          {
                public int Sum(int A, int B)
               {
                   return A + B;
               }
               public float Sum(float A, float B)
              {
                  return A + B;
              }
       }
      class Class2 : Class1
     {
           public int Sum(int A, int B, int C)
          {
                return A + B + C;
          }
     }
    class mClass
   {
         static void Main()
        {
              Class2 objclass2 = new Class2();
              Console.WriteLine(objclass2.Sum(10, 20));
              Console.WriteLine(objclass2.Sum(10.30f, 15.70f));
              Console.WriteLine(objclass.Sum(10, 20, 30));
              Console.Read();
       } 
     }
 }
Sourabh Sharma (150 Posts)


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>