Back to course

Constants and Readonly Fields

C# Zero to Hero: Comprehensive Programming Masterclass

Const vs Readonly

Both prevent the value from being changed after assignment.

Const

Value must be assigned at the time of declaration. It's fixed at compile time. csharp public const double PI = 3.14159;

Readonly

Can be assigned during declaration or inside the constructor. It's fixed at runtime. csharp public readonly string CreationTime;