3️⃣ C# Variables, Data Types & Type Systems
Estimated reading: 3 minutes 364 views

C# Constants – Define Fixed Values with const in C#


Introduction – Why Use Constants in C#?

In C#, constants are used to define immutable values that remain unchanged throughout the life of a program. Declaring constants helps prevent accidental changes and makes your code more reliable, readable, and maintainable.

In this guide, you’ll learn:

  • How to declare and use constants in C#
  • Differences between const and readonly
  • When and why constants are preferred
  • Common best practices and examples

Core Concept – What Are Constants?

A constant in C# is a value that is assigned at compile time and cannot be changed later. It must be declared with the const keyword and initialized when defined.

Syntax:

const data_type constant_name = value;

Example:

const double Pi = 3.14159;

Note: Constants are implicitly static and accessible without object instantiation.


Code Example – Declaring and Using Constants

using System;

class Circle
{
    const double Pi = 3.14159;

    static void Main()
    {
        double radius = 5;
        double area = Pi * radius * radius;

        Console.WriteLine($"Radius: {radius}");
        Console.WriteLine($"Area: {area}");
    }
}

Output:

Radius: 5
Area: 78.53975

const vs readonly

Featureconstreadonly
Assigned when?At compile timeAt runtime (in constructor)
MutabilityImmutable, cannot changeImmutable after constructor
Use CaseFixed, unchanging values (e.g., Pi)Runtime-initialized fixed values
Static?Implicitly staticMust be explicitly marked if static

Best Practice: Use const for universal constants (e.g., Pi, MaxSize) and readonly for configuration or runtime constants.


Tips, Pitfalls & Best Practices

Tip: Use all-uppercase naming convention for constants (MAX_USERS, DEFAULT_RATE) in some enterprise teams.

Pitfall: Avoid using const for values that might change in future versions — re-compilation is needed in every referencing assembly.

Best Practice: Place constants in a dedicated Constants class or static class for global access and organization.


Use Cases – Where to Use Constants

  • Mathematical constants like Pi, E
  • Limits like MAX_RETRIES, TIMEOUT
  • Configuration settings that never change
  • Message templates or version strings

Summary – Recap & Next Steps

C# constants are perfect for fixed values that never change. They’re compile-time safe, help prevent bugs, and improve code clarity.

Key Takeaways:

  • Use const for compile-time fixed values
  • Constants are implicitly static
  • Use readonly when the value is known only at runtime

Coming next: Dive into C# Data Types to understand how different values are categorized and stored.


FAQ – C# Constants

What is a constant in C#?
A const is a fixed value known at compile time and cannot be modified later.

Can I assign a constant inside a method?
Yes, but the value must still be fixed at compile time.

What’s the difference between const and readonly?
const is for compile-time constants, readonly for values set at runtime.

Are constants implicitly static?
Yes. Constants are treated as static by the compiler.

Can constants be public?
Yes. You can specify access modifiers like public const int Max = 100;.


Share Now :
Share

📦 C# Constants

Or Copy Link

CONTENTS
Scroll to Top