Read the syntax
struct defines a value type. Assigning one struct variable to another copies its fields rather than making two references to the same object.
A record struct combines value-type storage with generated record behavior such as value equality and display text. Prefer small, cohesive values and be aware that copying a large mutable struct can be surprising.
Syntax
struct StructName {\n // fields, properties, methods\n}Examples
Basic Struct
Defining and using a simple struct.
Point p = new Point(3, 4);
Console.WriteLine(p); // (3, 4)
public struct Point
{
public int X, Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
public override string ToString() => $"({X}, {Y})";
}- Defining and using a simple struct.
- struct defines a value type. Assigning one struct variable to another copies its fields rather than making two references to the same object.
- new Point(3, 4) stores two coordinates. The overridden ToString supplies (3, 4) when the point is printed.
Value Semantics: Copied, Not Referenced
The key difference from a class - assigning a struct copies its data completely.
Point original = new Point(1, 1);
Point copy = original; // this COPIES the entire struct
copy.X = 99;
Console.WriteLine(original.X); // 1 - unaffected by the change to copy
Console.WriteLine(copy.X); // 99
// Compare to a class, where "copy" would just be another reference
// to the SAME object, and changing copy.X would also change original.X
public struct Point
{
public int X, Y;
public Point(int x, int y) { X = x; Y = y; }
}- The key difference from a class - assigning a struct copies its data completely.
- struct defines a value type. Assigning one struct variable to another copies its fields rather than making two references to the same object.
- copy begins as a copy of original; changing copy.X to 99 leaves original.X at 1. The two printed values show independent value-type state.
record struct (C# 10+)
Combining record-style value equality and concise syntax with struct's value-type copy semantics.
Point p1 = new Point(3, 4);
Point p2 = new Point(3, 4);
Console.WriteLine(p1 == p2); // true - value equality, like a record
Console.WriteLine(p1); // Point { X = 3, Y = 4 }
public record struct Point(int X, int Y);- Combining record-style value equality and concise syntax with struct's value-type copy semantics.
- struct defines a value type. Assigning one struct variable to another copies its fields rather than making two references to the same object.
- Both record structs have coordinates 3 and 4, so == prints True. The generated string form displays the named coordinates.
Best practices
- Use structs for small, immutable data groupings (roughly 16 bytes or less is a common guideline) where value-copy semantics make sense, like coordinates or RGB colors
- Use classes instead of structs for anything larger or that represents an entity with identity (a Customer, an Order) rather than a simple value
- Be aware that passing a large struct around by value can hurt performance due to repeated copying - pass by "in" or "ref" if a large struct must avoid copies
- Consider record struct (C# 10+) when you want both value equality and value-type copy semantics together
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference