← Back to C# Course | Chapter 2: Variables & Types | Lesson 6 of 7

const Fields

In this page:

  1. const Fields

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

markup
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);
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.