---
source: https://iinventedlight.com/library/notes/csharp-structs.md
title: "C# structs vs classes"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates cited source lines"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: C#"
tags: ['csharp', 'dotnet', 'structs', 'value-types']
sources:
  - id: S1
    title: "Structure types - C# reference"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "C# classes - C#"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# 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.

```csharp
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 `object` or 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).
