Constants and readonly Fields
Distinguish const from static readonly.
Constants and readonly Fields is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Two Ways to Make Values Fixed
C# offers const and readonly for values that should not change. They look similar but behave very differently. This lesson compares them.
using System;
class Config
{
public const double Pi = 3.14159;
public static readonly DateTime StartedAt = DateTime.Now;
}
class Program
{
static void Main()
{
Console.WriteLine("Pi = " + Config.Pi);
}
}const: Compile-Time Constant
A const must be assigned a literal value at declaration and is baked in at compile time. It is implicitly static.
using System;
class Circle
{
public const double Pi = 3.14159;
public static double Area(double r) => Pi * r * r;
}
class Program
{
static void Main()
{
Console.WriteLine(Circle.Area(2).ToString("0.00"));
}
}const Must Be a Literal
Because it is resolved at compile time, a const can only hold a constant expression: numbers, strings, booleans, or other consts.
using System;
class Limits
{
public const int MaxRetries = 3;
public const int Timeout = MaxRetries * 1000; // const expression OK
}
class Program
{
static void Main()
{
Console.WriteLine(Limits.MaxRetries + ", " + Limits.Timeout);
}
}readonly: Runtime Constant
A readonly field is set once, either at declaration or in a constructor, and then cannot change. Its value can be computed at runtime.
using System;
class Session
{
public readonly string Id;
public Session(string id)
{
Id = id; // allowed: inside constructor
}
}
class Program
{
static void Main()
{
var s = new Session("abc-123");
Console.WriteLine("Session: " + s.Id);
}
}readonly Can Use Runtime Values
Unlike const, a readonly field can hold a value only known when the program runs, such as a generated id or current time.
using System;
class Order
{
public readonly Guid OrderId;
public Order()
{
OrderId = Guid.NewGuid();
}
}
class Program
{
static void Main()
{
var o = new Order();
Console.WriteLine("Has id: " + (o.OrderId != Guid.Empty));
}
}static readonly
Combine static readonly for a class-wide value computed once at runtime. This is the usual replacement for a const when the value is not a literal.
using System;
class AppInfo
{
public static readonly string[] Roles = { "admin", "user", "guest" };
}
class Program
{
static void Main()
{
Console.WriteLine("Roles: " + Roles());
}
static int Roles() => AppInfo.Roles.Length;
}Why const Can Be Risky Across Assemblies
Because const values are inlined into calling code at compile time, changing a public const in a library requires recompiling all callers. static readonly avoids this.
using System;
class Lib
{
public const int Version = 2; // inlined into callers
public static readonly int Build = 100; // read at runtime
}
class Program
{
static void Main()
{
Console.WriteLine(Lib.Version + "." + Lib.Build);
}
}Cannot Reassign Either One
After initialization, neither a const nor a readonly field can be changed. Attempting to do so is a compile error.
using System;
class Demo
{
public const int A = 10;
public readonly int B;
public Demo() { B = 20; }
// A = 11; // would not compile
// B = 21; // would not compile outside constructor
}
class Program
{
static void Main()
{
var d = new Demo();
Console.WriteLine(Demo.A + ", " + d.B);
}
}Choosing Between Them
Use const for true literals that never change (Pi, MaxInt). Use readonly when the value is computed at runtime or may differ per instance.
using System;
class Physics
{
public const double Gravity = 9.81; // universal literal
public readonly double Mass; // per object
public Physics(double mass) { Mass = mass; }
public double Weight() => Mass * Gravity;
}
class Program
{
static void Main()
{
var p = new Physics(10);
Console.WriteLine("Weight: " + p.Weight());
}
}readonly Reference Caveat
readonly locks the field, not the object it points to. A readonly list reference cannot be reassigned, but its contents can still change.
using System;
using System.Collections.Generic;
class Cart
{
public readonly List<string> Items = new List<string>();
}
class Program
{
static void Main()
{
var c = new Cart();
c.Items.Add("Pen"); // allowed: mutating the object
c.Items.Add("Book");
Console.WriteLine("Items: " + c.Items.Count);
}
}Putting It Together
A typical class uses const for fixed literals and readonly for values fixed per instance at construction time.
using System;
class Account
{
public const decimal MinBalance = 0m;
public readonly string Owner;
public Account(string owner)
{
Owner = owner;
}
}
class Program
{
static void Main()
{
var a = new Account("Rin");
Console.WriteLine(a.Owner + " min=" + Account.MinBalance);
}
}Quick Check
Test your understanding of const vs readonly.
Recap
const is a compile-time literal, implicitly static, and inlined into callers. readonly is set once at declaration or in a constructor and can use runtime values. Use const for true literals, static readonly for runtime-computed shared values, and remember readonly locks the reference, not the object.
using System;
class Demo
{
public const int Max = 100;
public readonly int Created;
public Demo(int c) { Created = c; }
}
class Program
{
static void Main()
{
var d = new Demo(7);
Console.WriteLine(Demo.Max + ", " + d.Created);
}
}Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Constants and readonly Fields” lesson free?
Yes — the full text of “Constants and readonly Fields” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Constants and readonly Fields”?
Distinguish const from static readonly. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Constants and readonly Fields” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Static Methods and Fields
- Static Classes for Utilities
- Constants and readonly Fields
- Static vs Instance Design