C# Fundamentals

C# is a object orienterd programming.It is a new way of designing and developing applications.It is different from structured languages like "C" and "Pascal" and allows you to view everything as an object.

The following are important features of OOP

Encapsulation
Inheritance
Polymoriphism


Encapsulation:Putting data and code together


Inheritance:Inheritance allows a class to be created from another class.The class being created is called derived class and the class being inherited is called as base class.


Polymoriphism:We have two types of polymorphisms in c#.

1.Compile-time polymorphism.
2.Run-time polymorphism.


A simple example program on compile time polymorphism:
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}

public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}

public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}


public class DrawDemo
{
public static int Main( )
{
DrawingObject[] dObj = new DrawingObject[4];

dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();

foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}

return 0;
}
}


output of the program is:

I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object.