C#.NET Polymorphism
Polymorphism means "Many forms".
In other words it can be defined as entities behave in different forms based on the given input i.e., whenever there is a change in the input the output on behavior also changes.
Polymorphism can be of two types.
1. Static Polymorphism or Method Overloading
2. Dynamic Polymorphism or Method Overriding
Example program for Method Overloading
Program
Output
Maths Add
Maths : 3
Maths : 7
Example program for Method Overriding
Program
Output
-2
Example program for Method Overriding and calling Parent method
Output
Maths A Add
In other words it can be defined as entities behave in different forms based on the given input i.e., whenever there is a change in the input the output on behavior also changes.
Polymorphism can be of two types.
1. Static Polymorphism or Method Overloading
2. Dynamic Polymorphism or Method Overriding
C#.NET Method Overloading
It is also known as "Compile time polymorphism”. It can have same method name and different parameters. It is also known as "Method Overloading".Example program for Method Overloading
Program
using System;
namespace Overloading
{
class Program
{
static void Main(string[] args)
{
Maths a = new Maths();
a.Add();//Maths
Add
a.Add(3);//Maths
: 3
a.Add(2, 5);//Maths
: 7
Console.ReadKey();
}
}
class Maths
{
public void Add()
{
Console.WriteLine("Maths Add");
}
public void Add(int a)
{
Console.WriteLine("Maths : " + a);
}
public void Add(int a, int b)
{
Console.WriteLine("Maths : " + (a + b));
}
}
}
Output
Maths Add
Maths : 3
Maths : 7
C#.NET Method Overriding
It is also known as "Run time polymorphism”. It can have same method name and same signature. It is also known as "Method Overriding”. Method Overriding can be used only when we have inheritance.Example program for Method Overriding
Program
using System;
namespace MethodOverriding
{
class Program
{
static void Main(string[] args)
{
y o = new y();
o.add(1, 3);
Console.ReadKey();
}
}
class x
{
public void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class y : x
{
public void add(int a, int b)
{
Console.WriteLine(a - b);
}
}
}
Output
-2
Example program for Method Overriding and calling Parent method
using System;
namespace Overriding
{
class Program
{
static void Main(string[] args)
{
MathsB b = new MathsB();
b.Add();
Console.ReadKey();
}
}
class MathsA
{
public void Add()
{
Console.WriteLine("Maths A Add");
}
}
class MathsB : MathsA
{
public void Add()
{//Calling parent method from child
method
base.Add();
Console.WriteLine("Maths B Add");
}
}
}
Output
Maths A Add
C#.NET Struct
In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.
Example program using C# Struct
Program
Output
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id :6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Example program using C# Struct
Program
using System;
namespace Struct
{
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1; /* Declare Book1 of type Book */
Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
Book1.title = "C
Programming";
Book1.author = "Nuha
Ali";
Book1.subject = "C
Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 specification */
Book2.title = "Telecom
Billing";
Book2.author = "Zara
Ali";
Book2.subject = "Telecom
Billing Tutorial";
Book2.book_id = 6495700;
/* print Book1 info */
Console.WriteLine("Book 1 title : {0}", Book1.title);
Console.WriteLine("Book 1 author : {0}", Book1.author);
Console.WriteLine("Book 1 subject : {0}", Book1.subject);
Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);
/* print Book2 info */
Console.WriteLine("Book 2 title : {0}", Book2.title);
Console.WriteLine("Book 2 author : {0}", Book2.author);
Console.WriteLine("Book 2 subject : {0}", Book2.subject);
Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);
Console.ReadKey();
}
}
}
Output
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id :6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
C#.NET Enum
Enum stands for enumeration. It makes programs cleaner and easier to read. It also causes no slowdown at runtime.
An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.
C# enumerations are value data type. In other words, enumeration contains its own values
Example program for enum
Program
Output
True
An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.
C# enumerations are value data type. In other words, enumeration contains its own values
Example program for enum
Program
using System;
namespace Enum
{
class Program
{
enum WorkingDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
};
static void Main()
{
// ... An enum local variable.
WorkingDays value = WorkingDays.Thursday;
// ... Test against known
Importance values.
if (value == WorkingDays.Friday)
{
Console.WriteLine("Not true");
}
else if (value == WorkingDays.Thursday)
{
Console.WriteLine("True");
}
Console.ReadKey();
}
}
}
Output
True
C#.NET Properties
The Set and Get methods of a property are contained inside the property declaration. You can control whether a property is read/write, read-only, or write-only by controlling whether a Get or Set method is included.
Example program for Set and Get properties
Program
Output
Simple Properties
N/A , 0
Joe , 99
Joe , 100
Example program for Set and Get properties
Program
using System;
namespace PropertiesExample
{
class Person
{
private string myName = "N/A";
private int myAge = 0;
// Declare a Name property of type
string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
// Declare an Age property of type
int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public static void Main()
{
Console.WriteLine("Simple Properties");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated
with the person:
Console.WriteLine(person.Name + " , " + person.Age);
// Set some values on the person
object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine(person.Name + " , " + person.Age);
// Increment the Age property:
person.Age += 1;
Console.WriteLine(person.Name + " , " + person.Age);
Console.ReadKey();
}
}
}
Output
Simple Properties
N/A , 0
Joe , 99
Joe , 100
C#.NET Readonly
A readonly field can be initialized either at the declaration or in a constructor.
Example Program for C#.NET read only
Program
Output
25
Example Program for C#.NET read only
Program
using System;
namespace ReadOnly
{
class Program
{
static void Main(string[] args)
{
Maths m = new Maths();
Console.ReadKey();
}
}
class Maths
{
const int a = 10;
readonly int b = 20;
public Maths()
{
b =
25; //Assigning value
to ready only variable in constructor
// a = 20; //Value cannot be
assigned to Const variable
Console.WriteLine(b);
}
}
}
25