What is the difference between a class and a structure?

Technology CommunityCategory: OOPWhat is the difference between a class and a structure?
VietMX Staff asked 3 years ago

Class:

  • A class is a reference type.
  • While instantiating a class, CLR allocates memory for its instance in heap.
  • Classes support inheritance.
  • Variables of a class can be assigned as null.
  • Class can contain constructor/destructor.

Structure:

  • A structure is a value type.
  • In structure, memory is allocated on stack.
  • Structures do not support inheritance.
  • Structure members cannot have null values.
  • Structure does not require constructor/destructor and members can be initialiazed automatically.