← All notes

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

Gotchas

Claims

Sources

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

Permanent ID: iil:note:jcafahmw

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.},
}

Markdown

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.