This lessons introduced structs and explains how to declare and use them in C#

Structs VS Classes

Structs are similar to classes but have subtle differences.

They are used as lightweight versions of classes that can help reduce memory management efforts when working with small data structures. In most situations, however, using a standard class is a better choice.

The principal difference between structs and classes is that instances of structs are values whereas instances of classes are references.

Thus when you pass a struct to a function by value you get a copy of the object, so changes to it are not reflected in the original because there are now two distinct objects but if you pass an instance of a class by reference then there is only one instance.

Declaring a struct

Structures, or structs, are defined with

  • struct keyword
  • followed by an identifier to name the structure.

Get hands-on with 1200+ tech skills courses.