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)
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.