const Fields
In this page:
const Fields
A const field is a compile-time constant — its value must be known at compile time and can never change afterward. Unlike readonly, const values are substituted directly into the compiled code wherever they're used. const is ideal for values like mathematical constants or fixed configuration limits.
Warning: A const cannot be assigned the result of a method call, since its value must be resolvable at compile time.
Example: const Fields
using System;
class Program
{
const double Pi = 3.14159;
const int MaxRetries = 3;
static void Main(string[] args)
{
Console.WriteLine(Pi);
Console.WriteLine(MaxRetries);
Console.WriteLine(Pi * 2 * 5);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: