C# Feature - readonly struct
Overview
Section titled “Overview”readonly struct marks a value type as immutable so the compiler can enforce that instance members do not mutate state.
Introduced In
Section titled “Introduced In”C# 7.2 (2017)
Before
Section titled “Before”public struct Distance{ public double Meters { get; }}public readonly struct Distance{ public double Meters { get; } public Distance(double meters) => Meters = meters;}Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Use
readonly structfor small immutable value objects. - Mutable fields defeat the purpose and should be avoided.