Creating a multiple methods in a class with same name but
different parameters and types is called as method overloading , method
overloading is the example of Compile time polymorphism.
Example of Method Overloading:-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace
ApplicationNamespace1
{
class methodOverloadingExampleClass
{
public void MethodA(int x, int y) //
MyMethod with int parameters
{
Console.WriteLine("Integers = "
+ x + y);
}
public void MethodA(char x, char y) //
MyMethod with char parameters
{
Console.WriteLine("Characters = "
+ x + y);
}
public void MethodA(int x, int y,int z) //
MyMethod with multiple int parameters
{
Console.WriteLine("Characters = "
+ x + y+z);
}
public void MethodA(string x, string y) //
MyMethod with string parameters
{
Console.WriteLine("Strings = "
+ x + y);
}
}
class Program
{
static void Main()
{
methodOverloadingExampleClass var = new methodOverloadingExampleClass();
var.MethodA(2, 3);
// This automatically calls int's
method
var.MethodA('a', 'b');
// This calls char's method
var.MethodA(2, 4,8); // This calls multiple int's method
var.MethodA("Method", "Overloading"); // This automatically calls string's method
Console.ReadLine();
}
}
}
|