Jan 29, 2010

Differnce between C# classes and structs

- Classes are reference types stored on the heap, structs are value types stored on the stack or inline(if they are part of another object that is stored on the heap)

- Classes support inheritance, structs don't support inheritance.

- Ex:
Class ExClass
{
public const string billday="Monday";
int custid;
}

struct ExStruct
{
public const string billday="Monday";
int custid;
}

ExClass ec=new ExClass();
ExStruct es=new ExStruct();

- If we need small datastructures, for performance reasons, we will go for structs instead of classes

- In classes, if we are not define any constructor, the compiler provides a default one otherwise not. But in structs, compiler always supplies a default no-parameter constructor.

- All classes derived from System.Object and all structs derived from System.ValueType.

No comments:

Post a Comment