C# structs vs classes
Answer: A struct is a value type: assigning or passing it copies the data. A class is a reference type: copies share one object.
public readonly struct Point(double x, double y) {
public double X { get; } = x;
public double Y { get; } = y;
}
var a = new Point(1, 2);
var b = a; // independent copy
readonly struct
Because structs have value semantics, Microsoft recommends making them immutable. If you don't initialize all fields in a struct, the compiler adds code to the constructor that initializes those fields to the default value. (S1)
When a class fits better
- The type has complex behavior or manages mutable state.
- You need inheritance.
- Instances represent a shared identity, and all references should see the same changes.
Gotchas
- Large structs are expensive to copy.
- Boxing a struct to
objector an interface allocates. - For information about ref struct and readonly ref struct types, see the ref structure types article. (S1)
Claims
- id: C1 claim: A struct type is a value type. source: S1 evidence: "A structure type (or struct type) is a value type that can encapsulate data"
- id: C2 claim: Because structs have value semantics, immutable structs are recommended. source: S1 evidence: "Because structure types have value semantics, we recommend you define immutable structure types."
- id: C3 claim: All data members of a readonly struct must be read-only. source: S1 evidence: "All data members of a readonly struct must be read-only as follows:"
- id: C4 claim: Classes fit types with complex behavior or mutable state. source: S2 evidence: "The type has complex behavior or manages mutable state."
- id: C5 claim: Classes support inheritance. source: S2 evidence: "Classes support inheritance."
Sources
- [S1] Structure types - C# reference. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] C# classes - C#. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Structure types - C# reference, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), C# classes - C#, CC BY 4.0 (dotnet/docs).
Cite this
iinventedlight. 2026. "C# structs vs classes." iinventedlight Library, iil:note:jcafahmw, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-structs/. License: CC-BY-4.0.
@misc{iil_note_jcafahmw,
author = {{iinventedlight}},
title = {{C\# structs vs classes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/csharp-structs/},
note = {Permanent ID iil:note:jcafahmw. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-structs.md.