C# Reference ยท Types and Literals
Structs in depth
Reference for C# structs: copy semantics, readonly struct, default values, constructors, mutability traps, equality, and when to choose a struct or class.
A struct is a value type: a variable holds the data itself, and assigning it or passing it to a method copies all of its fields. Two struct variables never alias each other, which makes structs ideal for small, self-contained values such as points, colors, money amounts and time spans.
Because copies are everywhere, mutable structs are a classic source of surprises. Modern C# gives you readonly struct, readonly members and record struct to make the safe style easy.
readonly struct Point(int x, int y) { public int X { get; } = x; public int Y { get; } = y; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[readonly] [ref] [partial] struct Name [: Interface1, Interface2] { fields, properties, methods, constructors, operators... }
| Feature | Notes |
|---|---|
| Inheritance | A struct cannot inherit from another struct or class (it derives from System.ValueType) and cannot be a base type. It can implement interfaces. |
| Parameterless constructor | Allowed since C# 10. new S() runs it; default(S) never does. Fields without initializers are zero. |
| Field initializers | Allowed when the struct declares a constructor (C# 11 also lets constructors leave fields unassigned; the compiler zeroes them). |
readonly struct |
Every field must be readonly and every property get-only or init. The compiler can then avoid defensive copies. |
readonly member |
A single method or property promises not to change the struct. |
ref struct |
Lives only on the stack (see ref struct). |
record struct |
Adds value equality, ToString and with (see Records in depth). |
Errors. The compiler rejects reading a struct local before all of its fields are assigned; the reliable fix is to initialize it through a constructor or default.
Usage notes
- Choose a struct when the type is small (roughly 16 bytes or less), logically a single value, immutable, and short-lived or stored inline in arrays. Choose a class for larger or identity-bearing objects, or anything that needs inheritance.
- Prefer immutability. A mutable struct in a
List<T>, a property or areadonlyfield is modified on a temporary copy, and the change is silently lost.readonly structremoves the problem. - Equality. The default
ValueType.Equalscompares fields, but uses reflection-like slow paths for some layouts. ImplementIEquatable<T>and==, or just userecord struct. ==is not provided for ordinary structs. The compiler rejectsa == buntil you define the operator.- Default value. Arrays of structs are zero-filled and no constructor runs for the elements, so the all-zero state must be valid.
- Boxing. Casting to
objector to an interface it implements allocates a copy; see Boxing and unboxing. Constrained generics (where T : struct, IComparable<T>) avoid boxing. - Large structs are slow to pass. Use
inparameters orref readonlyfor read-only access without copying. - Finalizers are not allowed, and a struct cannot contain a field of its own type.