---
title: "Difference between static, readonly, and constant in C#"  
description: "Difference between static, readonly, and constant in C#"  
author: "Ravi Vishwakarma"  
published: 2024-06-07  
updated: 2024-06-07  
canonical: https://www.mindstick.com/interview/33896/difference-between-static-readonly-and-constant-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 6 minutes  

---

# Difference between static, readonly, and constant in C#

In C#, static, read-only, and const are all keywords used to create members of classes or variables with different behaviors. They serve distinct purposes and have different behaviors.

#### Static:

1. Purpose: The static keyword is used to declare static members that belong to the type itself rather than to any specific instance of the type.
2. Behavior: Static members are shared among all instances of a class and we can access directly through the class name without creating an instance.
3. Initialization: They are initialized only once, typically at the time of the first access, and retain their value throughout the lifetime of the application.
4. Example: Commonly used for methods, fields, or properties that are not dependent on instance state, such as utility methods, constants, or shared resources.

## Example

```cs
public class MyClass
{
    public static int Count = 0; // Static field
    public static void IncrementCount() // Static method
    {
        Count++;
    }
}
```

#### Readonly:

1. **Purpose:** The readonly keyword is used to define fields that can only be assigned a value at the time of declaration or within the constructor of that class.
2. **Behavior:** Readonly fields cannot be modified after initialization, providing a way to create immutable fields that retain their value throughout the object's lifetime.
3. **Initialization:** Readonly fields can only be assigned a value during declaration or within the constructor of the class, and their value cannot be changed afterward.
4. **Example:** Useful for creating immutable fields or constants that are known at runtime but cannot be known at compile time (unlike const).

Example :

```cs
public class MyClass
{
    public readonly int MaxValue; // Readonly field

    public MyClass(int value)
    {
        MaxValue = value; // Can only be assigned in the constructor
    }
}
```

#### Const:

1. **Purpose:** The const keyword is used to declare constant members or variables, which are values that are known at compile time and cannot be changed afterward.
2. **Behavior**: Const values are replaced directly by their values at compile time wherever they are referenced in the code.
3. **Initialization:** Const fields must be initialized with a value at the time of declaration and cannot be assigned a value elsewhere.
4. **Example:** Used for defining values that are not expected to change during the execution of the program and can be known at compile time.

## Example -

```cs
public class Constants
{
    public const double PI = 3.14159; // Const field
}
```

####

#### Differences:

1. **Mutability:** Static fields can be modified after initialization, while readonly fields and constants cannot be changed after initialization.
2. **Initialization:** Static fields are initialized only once, typically at the time of the first access, while readonly fields and constants must be initialized either at the time of declaration or within the constructor.
3. **Value Assignment:** Readonly fields can be assigned a value either at declaration or within the constructor, while constants must be assigned a value at the time of declaration.
4. **Scope:** Static members belong to the type itself, while readonly fields and constants belong to individual instances of the type.

\
Use **static** for **shared members**, **readonly** for **immutable instance** fields, and **const** for **compile-time constants** whose values are known at **compile time**. Each serves a distinct purpose in C# programming and helps in designing **maintainable** and efficient code.

## Answers

### Answer by Ravi Vishwakarma

In C#, static, read-only, and const are all keywords used to create members of classes or variables with different behaviors. They serve distinct purposes and have different behaviors.

#### Static:

1. Purpose: The static keyword is used to declare static members that belong to the type itself rather than to any specific instance of the type.
2. Behavior: Static members are shared among all instances of a class and we can access directly through the class name without creating an instance.
3. Initialization: They are initialized only once, typically at the time of the first access, and retain their value throughout the lifetime of the application.
4. Example: Commonly used for methods, fields, or properties that are not dependent on instance state, such as utility methods, constants, or shared resources.

## Example

```cs
public class MyClass
{
    public static int Count = 0; // Static field
    public static void IncrementCount() // Static method
    {
        Count++;
    }
}
```

#### Readonly:

1. **Purpose:** The readonly keyword is used to define fields that can only be assigned a value at the time of declaration or within the constructor of that class.
2. **Behavior:** Readonly fields cannot be modified after initialization, providing a way to create immutable fields that retain their value throughout the object's lifetime.
3. **Initialization:** Readonly fields can only be assigned a value during declaration or within the constructor of the class, and their value cannot be changed afterward.
4. **Example:** Useful for creating immutable fields or constants that are known at runtime but cannot be known at compile time (unlike const).

Example :

```cs
public class MyClass
{
    public readonly int MaxValue; // Readonly field

    public MyClass(int value)
    {
        MaxValue = value; // Can only be assigned in the constructor
    }
}
```

#### Const:

1. **Purpose:** The const keyword is used to declare constant members or variables, which are values that are known at compile time and cannot be changed afterward.
2. **Behavior**: Const values are replaced directly by their values at compile time wherever they are referenced in the code.
3. **Initialization:** Const fields must be initialized with a value at the time of declaration and cannot be assigned a value elsewhere.
4. **Example:** Used for defining values that are not expected to change during the execution of the program and can be known at compile time.

## Example -

```cs
public class Constants
{
    public const double PI = 3.14159; // Const field
}
```

####

#### Differences:

1. **Mutability:** Static fields can be modified after initialization, while readonly fields and constants cannot be changed after initialization.
2. **Initialization:** Static fields are initialized only once, typically at the time of the first access, while readonly fields and constants must be initialized either at the time of declaration or within the constructor.
3. **Value Assignment:** Readonly fields can be assigned a value either at declaration or within the constructor, while constants must be assigned a value at the time of declaration.
4. **Scope:** Static members belong to the type itself, while readonly fields and constants belong to individual instances of the type.

\
Use **static** for **shared members**, **readonly** for **immutable instance** fields, and **const** for **compile-time constants** whose values are known at **compile time**. Each serves a distinct purpose in C# programming and helps in designing **maintainable** and efficient code.


---

Original Source: https://www.mindstick.com/interview/33896/difference-between-static-readonly-and-constant-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
