Skip to content
Tutorials Center
My learning

Topics

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.

C#
public readonly struct Point(int x, int y)
{
    public int X { get; } = x;
    public int Y { get; } = y;
}

Try it

C# Copy semantics

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.

C# readonly struct and record struct

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.

C# A mutable struct trap

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

C#
[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 Equals compares fields, but through reflection in some cases. Implement IEquatable<T> (a record struct does it for you) for performance in collections.
  • Prefer immutability. A readonly struct avoids defensive copies when you pass it as in or call members on a readonly field. 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 object or 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 struct types (Span<T>) can never be boxed or stored on the heap.