Wednesday, November 2, 2011

What is the difference between a class and a structure?

Class:

  1. A class is a reference type.
  2. The members of a class are private by default.
  3. While instantiating a class, CLR allocates memory for its instance in heap.
  4. Classes support inheritance.
  5. Variables of a class can be assigned as null.
  6. Class can contain constructor/destructor.

Structure:
  1. A structure is a value type.
  2. The members of a structure are public by default
  3. In structure, memory is allocated on stack.
  4. Structures do not support inheritance.
  5. Structure members cannot have null values.
  6. Structure does not require constructor/destructor and members can be initialiazed automatically.

No comments:

Post a Comment