Advertisement
Beginner C#, Fundamentals

What is the difference between const and readonly?

Short answer

const is a compile-time constant embedded into callers and must be initialised inline; readonly is set once at runtime (declaration or constructor) and can vary per instance.

Save C# mock interview
Advertisement
AModel answer
const is a compile-time constant embedded into callers and must be initialised inline; readonly is set once at runtime (declaration or constructor) and can vary per instance.
public const double Pi = 3.14159;
public readonly DateTime Created = DateTime.UtcNow;
Real-world example
const for fixed math constants; readonly for values known only at construction, like an injected config value.