---
title: "difference between constant and read only in C#"  
description: "difference between constant and read only in C#"  
author: "Anupam Mishra"  
published: 2016-01-24  
updated: 2016-01-24  
canonical: https://www.mindstick.com/forum/33923/difference-between-constant-and-read-only-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# difference between constant and read only in C#

Hi..Please give me a brief [introduction](https://www.mindstick.com/articles/13122/an-introduction-to-network-cables) about [constant](https://www.mindstick.com/forum/34714/difference-between-statics-vs-constant-in-c-sharp) and read only in c# and its differences.\
Thank you.

## Replies

### Reply by Shiva Shukla

**Constant:-**It is a compile-time constant. A field or local variable which is declared as constant can be initialized with a constant expression which must be fully evaluated at compile time.**\**

You can mark Constants as public, private, protected, internal, or protected internal access modifiers.\
\
**When to use\** \
If you think that the value of field or local variable is never changed.\

```
const int x = 10;  const int y = 20;  const int z = x + y;  Console.WriteLine("The value of x :" + x);  Console.WriteLine("The value of y :" + y);  Console.WriteLine("The value of z :" + z);     int a = 15;  const int b = x + a;  Console.WriteLine("The value of b :" + b);  //The variable 'a' is assigned but its value is never used  
```

\
**ReadOnly:-**\
It is same as Constant but it is run time constant. I mean to say that a ReadOnly field or local variable can be initialized either at the time of declaration or inside the constructor of same class. That is why we called it run time constant.**\**

```
public class MyClassProgram  {     readonly int x = 10;     public MyClassProgram()     {        //changed the value in constructor        x = 20;     }  }  
```

As you know that we cannot declare Constant as static but we can do it for readonly explicitly. By default it is not static. It can be applied to value type and reference type [which initialized by using the new keyword)] both and also with delegate and event \
\
**When to use\** \
If you think, you need to change the value of variable or field at run time inside the calling constructor, then you need to use the readonly modifier.\

**\****\**


---

Original Source: https://www.mindstick.com/forum/33923/difference-between-constant-and-read-only-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
