• Home
  •  Compile Time Polymorphism, Method and Operator Overloading

 Compile Time Polymorphism, Method and Operator Overloading

Agenda

Compile Time Polymorphism in C#

Example for Method Overloading by changing the no and data type of Arguments:

public class MethodOverLoading {

public void add(int a, int b){

Console.WriteLine(a+b);

}

public void add(int a, int b, int c){

Console.WriteLine(a+b+c);

}

public void add(double a, double b){

Console.WriteLine(a+b);

}

static void Main() {

MethodOverLoading obj = new MethodOverLoading();

obj.add(100, 200);

obj.add(15, 25, 35);

obj.add(101.234, 23.456);

} }

Method Overloading in C#

Example for Method Overloading by changing the order of Arguments:

class Program {
public void studentIdentity(String name, int id)

{

Console.WriteLine("Name1 : " + name + ", "

+ "Id1 : " + id);

}
public void studentIdentity(int id, String name)

{

Console.WriteLine("Name2 : " + name + ", "

+ "Id2 : " + id);

}

static void Main()

{

Program obj = new Program();

obj.studentIdentity("Rahul", 1);

obj.studentIdentity(2, "Rajesh");

}

}
 

Operator Overloading in C#

Operator Overloading: Some operators in C# behave differently with different operands.
For example, + operator is overloaded to perform numeric addition as well as string
concatenation.

Example for Operator Overloading:

public class MethodOverLoading {

public int add(int a, int b){

int result = a+b;

return result;

}

public string add(string a, string b){

string result = a+b;

return result;

}

static void Main() {

MethodOverLoading obj = new MethodOverLoading();

Console.WriteLine(obj.add(10, 20)); //30

Console.WriteLine(obj.add(“Hello “, “C#”)); //Hello C#

} }


 public int plusMethod(int i, int j)
{
int result = i + j;
return result;
}
public string plusMethod(string i, string j)
{
string result = i + j;
return result;
}

public void studentIdentity(String studentName, int rollNo)
{
Console.WriteLine("Name1: "+ studentName+ " ," + "Roll1: "+ rollNo);
}
public void studentIdentity(int rollNo, String studentName)
{
Console.WriteLine("Name2: " + studentName + " ," + "Roll2: " + rollNo);
}

public void add(int a, int b) {
Console.WriteLine(a+b);
}
public void add(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
public void add(double a, double b)
{
Console.WriteLine(a + b);
}

Program obj = new Program();
Console.WriteLine(obj.plusMethod(23, 34)); //57
Console.WriteLine(obj.plusMethod("Learn C# ", "from Scratch")); //Learn C# from Scratch

obj.add(20, 25);
obj.add(10, 30, 50);
obj.add(200.34, 300.988);
obj.studentIdentity("Rahul", 2);
obj.studentIdentity(4, "Rajesh");

public class MethodOverLoading {
public void add(int a, int b){
Console.WriteLine(a+b);
}
public void add(int a, int b, int c){
Console.WriteLine(a+b+c);
}
public void add(double a, double b){
Console.WriteLine(a+b);
}
static void Main() {
MethodOverLoading obj = new MethodOverLoading();
obj.add(100, 200);
obj.add(15, 25, 35);
obj.add(101.234, 23.456);
} }

public int add(int a, int b)
{
int result = a + b;
return result;
}

public string add(string a, string b)
{
string result = a + b;
return result;
}

public void studentIdentity(String name, int id)
{

Console.WriteLine("Name1 : " + name + ", "
+ "Id1 : " + id);
}
public void studentIdentity(int id, String name)
{

Console.WriteLine("Name2 : " + name + ", "
+ "Id2 : " + id);
}

Program obj = new Program();
obj.studentIdentity("Rahul", 1);
obj.studentIdentity(2, "Rajesh");

Console.WriteLine(obj.add(1, 2));
Console.WriteLine(obj.add("Hello ", "C#"));