Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.75 KB

File metadata and controls

46 lines (34 loc) · 1.75 KB

Struct VS Class

The main difference between the 2 is that, STRUCTS are VALUE TYPES, while CLASSES are REFERENCE TYPES.

Bonus

The Memory is divided in two areas: Stack(local) and Heap(instance).

On the Stack are stored the value types. On the Heap, the reference types are stored.

INHERITANCE:

  @ STRUCTS : - Don't support inheritance; 
              - A Struct is not allowed to inherit from another struct or class.
              - Function member of the struct cannot be virtual or abstract.
  @ CLASSES : - Support inheritance; 
              - can inherit from another class.
              - Function member of the class can be virtual or abstract.

NULL REFERENCE:

  # STRUCTS : Cannot have null references;
  # CLASSES : Can have null references;

PROTECTED ACCESS MODIFIER:

  * The data member of a class can be protected.
  * The data member of struct can’t be protected.

LOGIC:

  $ STRUCTS : Light on logic, they support data;
  $ CLASSES : Contain complex logic behind;

PARAMETERLESS CONSTRUCTOR :

  * A CLASS can have a parameterless constructor.
  * A STRUCT does not contain parameter less constructor, but can contain Parameterized constructor or static constructor.

New Keyword:

  * Classes use new keyword for creating instances.
  * Structs can create an instance, with or without new keyword.

INTERFACES :

  Just like classes, a struct can also implement from an interface.

DEFAULT VALUES:

  *Default value for classes: null;
  *Default value for struct can not be null. The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null