Back to results for “Fundamentals”
C# › Fundamentals

What is the difference between const and readonly?

Beginner

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.