Read the syntax
Keep a value type’s own state fixed while recognising that referenced objects can still be mutable.
MoveX returns another Point instead of assigning to the original fields.
What it is and why to use it
A small value such as a coordinate can be easier to reason about when its own fields cannot change after construction. readonly struct expresses that design for a value type. It also helps the compiler reason about read-only operations. It does not recursively freeze every object referenced by its fields.
How it works
A readonly struct requires instance fields to be readonly and restricts its own mutating members. Values are copied by value according to normal struct rules. If a field or property holds a mutable class instance, copying the struct still copies a reference to that same object. Treat deep immutability as a separate domain design.
Common mistakes and limitations
Do not place a mutable List inside a readonly struct and claim the entire object graph is immutable. Do not assume default struct values satisfy constructor-only validation. Use the smallest suitable representation and avoid copying unnecessarily large structs.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
readonly struct Point { public int X { get; } public Point(int x) => X = x; }Examples
Create a new value for a changed coordinate
Keep the original coordinate while calculating a moved point.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var start = new Point(2, 3);
var moved = start.MoveX(4);
Console.WriteLine($"{start.X}, {start.Y}");
Console.WriteLine($"{moved.X}, {moved.Y}");
readonly struct Point(int x, int y) {
public int X { get; } = x;
public int Y { get; } = y;
public Point MoveX(int amount) => new(X + amount, Y);
}
- The get-only properties hold the struct’s fixed coordinate values.
- MoveX returns another Point instead of assigning to the original fields.
- Both values remain available, which is useful for small mathematical or domain values.
- Expected output: 2, 3 6, 3
- Keep small value representations simple and stable.
Expose the limit of shallow readonly state
Observe a mutable list referenced by two struct copies.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var items = new List<int> { 1 };
var first = new Bag(items);
var second = first;
second.Items.Add(2);
Console.WriteLine(first.Items.Count);
readonly struct Bag(List<int> items) {
public List<int> Items { get; } = items;
}
- Copying Bag copies its list reference, not the list’s contents.
- The readonly struct cannot reassign its stored reference, but the List object can still be mutated.
- Both copies observe the added item. Use an actually immutable data representation when shared mutation is unacceptable.
- Expected output: 2
- Keep small value representations simple and stable.
Best practices
- Keep small value representations simple and stable.
- Distinguish a fixed reference from an immutable referenced object.
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