Structs (C# Programming Guide)

Structs are defined by using the struct keyword, for example:

public struct PostalAddress
    // Fields, properties, methods and events go here...

Structs share most of the same syntax as classes. The name of the struct must be a valid C# identifier name. Structs are more limited than classes in the following ways:

  • Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
  • A struct cannot declare a parameterless constructor (a constructor without parameters) or a finalizer.
  • Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.
  • Structs are value types, unlike classes, which are reference types.
  • Unlike classes, structs can be instantiated without using a new operator.
  • Structs can declare constructors that have parameters.
  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from ValueType, which inherits from Object.
  • A struct can implement interfaces.
  • A struct cannot be null, and a struct variable cannot be assigned null unless the variable is declared as a nullable value type.

See also