Wednesday, July 9, 2008

.Net Common Type System CTS

CTS: Common Type System, which describes how a Type should be defined in order to be hosted by CLR. 5 Types are available.

Class Type: Building block of OOS

Structure Type: User defined type. Can contain constructors and Methods
struct point
{
public int xPos,yPos;
public Point(int x, int y) //Constructor
{
xPos = x; yPos = y;
}

public void ShowMe() //methods
{
Console.WriteLine("{0},{1}",xPos,yPos);
}
}

Enumeration Type:Provides an efficient way to define a set of named integral constants that may be assigned to a variable.

enum MachineState
{
PowerOff = 0,
Running = 5,
Sleeping = 10,
Hibernating = Sleeping + 5
}

Delegate Type: A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value

namespace test
{
public delegate int mydelg( int x, int y);

public class myClass
{
public int Add( int x, int y)
{return x+y;}
public int Sub( int x, int y)
{return x-y;}
}

class MyProgram
{
static void Main(string[] args)
{
mydelg d= new mydelg(myClass.Add);
int result = d(1,2);
d+= new mydelg(myClass.Sub); // can call more than 1 method
}
}

} //namespace

Interface Type: An interface contains only the signatures/declaration of methods, delegates or events. The implementation of the methods is done in the class that implements the interface.
public interface IDraw
{
void Draw();
}
public class DrawME:Draw
{
public void Draw()
{
Console.WriteLine("Hello");
}
}

No comments:

Post a Comment