Constants and Read-Only Fields

Learn to distinguish between compile-time constants and runtime read-only fields to control data immutability in C#.

C# provides two primary mechanisms for creating immutable members: constants and read-only fields. While they appear similar, they differ significantly in when their values are assigned and how they are stored in memory.

Constants

A constant is an immutable field with a value assigned during compilation. Constants are immutable and cannot be reassigned or modified during program execution.

We declare constants using the const keyword: