---
title: "What is the difference between readonly, const, and static?"  
description: "What is the difference between readonly, const, and static?"  
author: "Anubhav Sharma"  
published: 2025-06-19  
updated: 2025-06-24  
canonical: https://www.mindstick.com/forum/161729/what-is-the-difference-between-readonly-const-and-static  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# What is the difference between readonly, const, and static?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between `readonly`, `const`, and `static`?

## Replies

### Reply by Ponu Maurya

## `const`

- Compile-time constant.
- Must be assigned at the time of declaration.
- Implicitly `static`.
- Cannot be changed after compilation.
- Value is **inlined** wherever it's used — changing it requires recompilation of all dependent assemblies.

```cs
public class Example
{
    public const double Pi = 3.14159;  // Compile-time constant
}
```

**Use when:** Value is truly constant and known at compile time (like Pi, conversion factors, etc.)

## `readonly`

- Run-time constant.
- Can be assigned in the declaration or **constructor**.
- Value can differ between instances.
- Ideal for values that should not change after the object is created.

```cs
public class Example
{
    public readonly int Id;

    public Example(int id)
    {
        Id = id;  // Allowed in constructor
    }
}
```

**Use when:** Value should be set once at runtime (e.g., ID, creation timestamp).

## `static`

- Belongs to the **type itself**, not to any instance.
- Shared across all instances.
- Can be used with fields, methods, constructors, classes, etc.

```cs
public class Example
{
    public static int Counter = 0;

    public Example()
    {
        Counter++;  // Shared by all instances
    }
}
```

**Use when:** You want a member that is shared across all instances (like configuration, counters, etc.)

## Combined Usage

You can combine `static` with `readonly`:

```cs
public static readonly string AppName = "MyApp";
```

Means: shared across all instances, value set at runtime, but not modifiable afterward.

## Summary Table

| Keyword | Assigned When | Modifiable? | Scope | Use For |
| --- | --- | --- | --- | --- |
| `const` | Compile-time | No | Static only | Hardcoded values |
| `readonly` | Runtime (in ctor) | After assigned | Per-instance or static | Init-once values |
| `static` | Any time | Yes (if not `readonly`) | Type-wide | Shared data/methods across class |


---

Original Source: https://www.mindstick.com/forum/161729/what-is-the-difference-between-readonly-const-and-static

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
