C# Reference ยท Keywords and Statements
The struct keyword
Reference for the C# struct keyword: value semantics, readonly structs, record structs, default values, boxing and when to choose a struct over a class.
struct declares a value type. A variable of a struct type contains the data itself, not a reference to it, so assigning one struct to another copies every field. Structs are a good fit for small, immutable pieces of data that behave like numbers: points, colors, money amounts, time spans.
A struct cannot inherit from another struct or class, but it can implement interfaces. Every struct has an implicit parameterless state, default(T), in which all fields are zero, false or null. Because that state always exists, you cannot rely on a constructor having run.
public 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.
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
[modifiers] struct Name [(parameters)] [: Interface, ...] { // fields, properties, methods, constructors, operators ... } readonly struct Name { ... } // every member is read-only ref struct Name { ... } // stack-only record struct Name(int A, int B); // C# 10: value-type record readonly record struct Name(int A);
| Feature | Rule for structs |
|---|---|
| Inheritance | None, except implicit System.ValueType; no virtual or protected members. |
| Parameterless constructor | Allowed since C# 10; new T() runs it, but default(T) and arrays skip it. |
| Field initializers | Allowed when the struct has a constructor (C# 10+). |
this |
In constructors, all fields must be assigned before returning (relaxed in C# 11: unassigned fields are zeroed). |
| Null | A struct cannot be null; use T? (Nullable<T>). |
Usage notes
- Equality: the default
Equalscompares fields, but through reflection in some cases. ImplementIEquatable<T>(arecord structdoes it for you) for performance in collections. - Prefer immutability. A
readonly structavoids defensive copies when you pass it asinor call members on areadonlyfield. Mutating a copy by accident is the commonest struct bug, as the third example shows. - Size: keep structs small (roughly 16 bytes or less) unless they are passed by
ref/in; large structs make every copy expensive. - Boxing: converting a struct to
objector to an interface it implements allocates. Generic constraints (where T : struct, IFoo) avoid it. - Where they live: locals and fields embed the data directly; a struct inside a class lives on the heap with that class.
- Default state: design so that
default(T)is a valid (or clearly invalid) value. ref structtypes (Span<T>) can never be boxed or stored on the heap.