Const vs Static vs Readonly in C#

The const, static, and readonly keywords in C# serve different purposes for declaring fields and members. Understanding their differences is crucial for choosing the right approach based on your specific needs.

Const

Constant fields are compile-time constants that cannot be modified after declaration. They must be assigned a value at the time of declaration and are implicitly static.

const int a = 5;

Example

using System;

class Constants {
   public const int MaxValue = 100;
   public const string AppName = "MyApp";
   public const double Pi = 3.14159;

   public static void Main() {
      Console.WriteLine("Max Value: " + MaxValue);
      Console.WriteLine("App Name: " + AppName);
      Console.WriteLine("Pi: " + Pi);
   }
}

The output of the above code is −

Max Value: 100
App Name: MyApp
Pi: 3.14159

Static

Static fields belong to the class itself rather than to any specific instance. They are shared among all instances of the class and can be modified at runtime.

static int a = 10;

Example

using System;

class Counter {
   public static int count = 0;
   public int instanceId;

   public Counter() {
      count++;
      instanceId = count;
   }

   public void DisplayInfo() {
      Console.WriteLine("Instance ID: " + instanceId + ", Total Count: " + count);
   }
}

class Program {
   public static void Main() {
      Counter c1 = new Counter();
      Counter c2 = new Counter();
      Counter c3 = new Counter();
      
      c1.DisplayInfo();
      c2.DisplayInfo();
      c3.DisplayInfo();
   }
}

The output of the above code is −

Instance ID: 1, Total Count: 3
Instance ID: 2, Total Count: 3
Instance ID: 3, Total Count: 3

Readonly

Readonly fields can be initialized at declaration or within a constructor. Once set, they cannot be modified, but their value can be determined at runtime.

readonly int a;

Example

using System;

class Demo {
   readonly int readonlyField;
   readonly DateTime creationTime = DateTime.Now;

   public Demo(int value) {
      readonlyField = value;
   }

   public void Display() {
      Console.WriteLine("Readonly Field: " + readonlyField);
      Console.WriteLine("Creation Time: " + creationTime);
   }
}

class Program {
   public static void Main() {
      Demo d1 = new Demo(25);
      Demo d2 = new Demo(50);
      
      d1.Display();
      d2.Display();
   }
}

The output of the above code is −

Readonly Field: 25
Creation Time: 12/15/2023 10:30:45 AM
Readonly Field: 50
Creation Time: 12/15/2023 10:30:45 AM

Comparison

Feature Const Static Readonly
Value Assignment At compile time only Can be changed at runtime At declaration or in constructor
Memory Location Replaced with literal value Shared among all instances Per instance or static
Initialization At declaration At declaration or runtime At declaration or constructor
Modifiable No Yes No (after initialization)

Key Differences Example

using System;

class ComparisonExample {
   public const int ConstValue = 100;
   public static int StaticValue = 200;
   public readonly int ReadonlyValue;

   public ComparisonExample(int value) {
      ReadonlyValue = value;
   }

   public static void Main() {
      ComparisonExample obj1 = new ComparisonExample(300);
      ComparisonExample obj2 = new ComparisonExample(400);

      Console.WriteLine("Const Value: " + ConstValue);
      Console.WriteLine("Static Value: " + StaticValue);
      Console.WriteLine("Obj1 Readonly: " + obj1.ReadonlyValue);
      Console.WriteLine("Obj2 Readonly: " + obj2.ReadonlyValue);

      // Modify static value
      StaticValue = 999;
      Console.WriteLine("Modified Static Value: " + StaticValue);
   }
}

The output of the above code is −

Const Value: 100
Static Value: 200
Obj1 Readonly: 300
Obj2 Readonly: 400
Modified Static Value: 999

Conclusion

Use const for compile-time constants, static for class-level variables that can change, and readonly for instance-specific values that should remain unchanged after initialization. Each serves distinct purposes in creating robust and maintainable C# applications.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements