What is Method Overloading?


What is Method Overloading?

What is Method Overloading?

Method Overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or one in base class and another in the derived class.

The signature of a function is determined by 3 factors:

a) Number of arguments received by the function.
b) Data types of the parameters/arguments.
c) Position/order of the arguments.

The signature of a function never depends upon the return type. Two functions differ only in their return type cannot be overloaded.

Thanks & Regards
Paritosh Mohapatra


Comments

Author: Phagu Mahato09 Jan 2014 Member Level: Gold   Points : 7

When we are overloading a method of the base class in derived class are know as Method overloading . Method overloading allows us to write different version of the same method in a class or derived class. Example of overloaded methods in C Sharp


using System;

class Program
{
public static void Main()
{
DispalyString(string.Empty);
DispalyString("Class");
}

static void DispalyString(string name)
{
if (name == string.Empty)
{
Console.WriteLine("Prem");
}
else
{
Console.WriteLine(name);
}
}
}
Another Example of Method Overloading
using System;

namespace Methodoverloading
{

class Example1
{

public int Sum(int X, int Y)
{
return X + Y;
}

public float Sum(int X, float Y)
{
return X + Y;
}
}

class Class2 : Example1
{
public int Sum(int X, int Y, int Z)
{
return X + Y + Z;

}
}

class MainClass
{
static void Main()
{

Class2 obj = new Class2();

Console.WriteLine(obj.Sum(15, 25));

Console.WriteLine(obj.Sum(16, 15, 45));

Console.Read();
}

}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: