-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Compile Time Polymorphism in C#
- Method Overloading in C#
- Operator Overloading in C#
Compile Time Polymorphism in C#
- Polymorphism is one of the features provided by Object Oriented Programming. It simply means occurring in more than one form. In other word, the same entity (method or operator) can perform different operations in different scenarios.
- Polymorphism derived from two Greek words, Poly-means Many and Morphs - means ways; So polymorphism means many ways.
-
There are two types of Polymorphism is available in C#.
a) Static/Compile Time Polymorphism (Method/Operator Overloading)
b) Dynamic/Run-Time Polymorphism (Method Overriding) -
Method Overloading: Two are more methods having same name in the same class but they differ in following ways.
a) Number of Arguments
b) Type of Arguments
c) Order of Arguments
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 Scratchobj.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#"));