Friday, July 11, 2008

1. Classes are reference types and structures are value types.Since classes are reference type, a class variable can be assigned null.But we cannot assign null to a struct variable, since structs are value type.
struct AStruct
{
int aField;
}
class AClass
{
int aField;
}
class MainClass
{
public static void Main()
{
AClass b = null; // No error.
AStruct s = null; // Error [ Cannot convert null to 'AStruct' because it is a value type ].
}
}

2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.

3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ).

4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference.

5. You cannot have instance Field initializers in structs.But classes can have initializers.

class MyClass
{
int myVar =10; // no syntax error.
public void MyFun( )
{
// statements
}
}
struct MyStruct
{
int myVar = 10; // syntax error.
public void MyFun( )
{
// statements
}
}

6. Classes can have explicit parameterless constructors. But structs cannot have explicit parameterless constructors.
class MyClass
{
int myVar = 10;
public MyClass( ) // no syntax error.
{
// statements
}
}
struct MyStruct
{
int myVar;
public MyStruct( ) // syntax error.
{
// statements
}

7. Classes must be instantiated using the new operator. But structs can be instantiated without using the new operator.

MyClass aClassObj; // MyClass aClassObj=new MyClass(); is the correct format.
aClassObj.myVar=100;//NullReferenceException(because aClassObj does not contain a
// reference to an object of type myClass).

MyStruct aStructObj;
aStructObj.myVar=100; // no exception.

8. Classes support inheritance.But there is no inheritance for structs.( structs don't support inheritance polymorphism )So we cannot have a base structure and a derived structure.
Example 1:
struct MyStruct
{
int aStructVar;
internal void aStructMethod()
{
// statements
}
}
class MyClass : MyStruct // Syntax error.
{
int aClassVar;
int aClassMethod()
{
// statements
}
}

Example 2:
class MyClass
{
int aClassVar;
int aClassMethod()
{
// statements
}
}
struct MyStruct : MyClass // Syntax error.
{
int aStructVar;
internal void aStructMethod()
{
// statements
}
}

9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.

10. It is not mandatory to initialize all Fields inside the constructor of a class. But all the Fields of a struct must be fully initialized inside the constructor.

class MyClass //No error( No matter whether the Field ' MyClass.myString ' is initialized or ot ). {
int myInt;
string myString;
public MyClass( int aInt )
{
myInt = aInt;
}
}
struct MyStruct // Error ( Field ' MyStruct.myString ' must be fully assigned before it leaves the constructor ).
{
int myInt;
string myString;
public MyStruct( int aInt )
{
myInt = aInt;
}
}