Difference between Class and Structure in C#

  • 1. Classes are Reference types and Structures are Values types.
  • 2. Classes will support an Inheritance whereas Structures won’t.
  • 3. Classes can have explicitly parameterless constructors whereas structures can’t
  • 4. It is not possible to declare destructor in structure but in class it is possible.
  • 5. All struct types implicitly inherit from System.ValueType whereas all classes inherits from System.Object

When to Use Structure and Class?

defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

In all other cases, you should define your types as classes.

C# - Frequently Asked Interview Questions