The const, read-only, and static read-only in C# are keywords used to define a constant, a read-only, and static read-only types of variables. These variables are used in a class so that the caller class cannot update the values of these variables once the values are assigned. In this post, learn the difference between const vs. read-only vs. static read-only and how to use them in a C# application.
Const in C#
The const keyword declares a constant type variable. That means a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default, a const is static, and we cannot change the value of a const variable throughout the entire program. So constant variables are useful when you already know a variable's value and know it will never change in the entire program.

Here I have created a class named Variables and defined all three variables, so first, let's play with a const type variable.

Here I tried to reassign the value of the const variable, but it gave me an error, "A const field requires a value to be provided". So, OK, now I initialize a value for this variable and try to change it further in the cl

Here I have created a static constructor, a default constructor, a parameterized constructor, and a Simple Method. I tried to change the value of the const variable everywhere, but once I assign the value, I cannot change it again since when I do, it gives me a compile time error, as you can see in the snapshot above.
Now let's move on to the read-only keyword.
Readonly in C#
Readonly is the keyword whose value we can change during runtime or assign it at run time but only through the non-static constructor. Not even a method. Let's see.

Here first, I try to initialize the value in the static constructor. It gives me an error. Which you can see above. Now I try to change the value in a method, see what happens.

Here, it also gives an error that you can only assign a value through a variable or a constructor. Now try to change the value in the default constructor.

The snapshot above shows that it's built successfully without an error, warning, or message. Let's check if there is a runtime error. OK.

Here we can see no runtime error, and the value was assigned successfully to the Readonly variable. Now one gotcha is direct that you have assigned the value. Can you change this value again ??? Let's try to change the value again.

Here I created a parameterized constructor and created a new object, passing a value as "Hello Frend'z," As I built it, it gave me the result "Build Succeeded." Now let's move ahead and check for a runtime error.

See guys. There is no runtime error !! The value can be changed again and again through a constructor.
Here is an article on when to use readonly vs. const in C#: Readonly and Constant variables in C#.
Now move ahead to Static Readonly variables.
Static ReadOnly in C#
A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. So it can change only once at runtime. Let's understand it practically.

In the preceding, you can see that I used two variables, one is not assigned, another is assigned, and the static constructor. Now in the static constructor, you can see that the unassigned variable is being assigned, and the assigned value is being changed. And there is no compile time error. Further, I try to change this variable's value again. See what happened.

As you can see above, I created a Default, Parameterized Constructor, and Method and tried to change the value again here. But I am getting a compile-time error for all.
Summary
This article taught you the difference between const vs. read-only vs. static read-only in C#. You also learned when to use these variables in your program.


Raji RajasekharanPosted May 6, 2025, 2:19 PM EDT
This is a very nice article, very well explained in detail. One small correction: please change the spelling from "cost" to "const".
Volodymyr PukhaPosted Jul 20, 2022, 3:34 PM EDT
Readonly last example is incorrect, it misleads. Readonly variables cannot be changed once it is initialized during variable declaration or assigned in the constructor. "See guys. There is no runtime error !! And the value can be changed again and again through a constructor.". You didn't change the re value in the var instance of Variables. What you did is created another object using different constructor and assigned there different value to re variable of instance objVar.
Spandana ReddyPosted Aug 26, 2021, 1:42 AM EDT
Explanation is very apt.
jubula samalPosted Mar 29, 2020, 1:20 AM EDT
Beautiful. Appreciate your effort to compose it so nicely.
RahbarKhanPosted Jan 27, 2020, 6:20 AM EST
I was asked this question in today's interview (:
Vikas RaoPosted Jan 17, 2020, 11:31 AM EST
Thanks Sir
Akshay DalviPosted Jan 9, 2020, 7:23 PM EST
Thanks for your efforts !!
Anil GangwarPosted Jun 23, 2019, 9:25 PM EDT
Very Good explained with great examples
Kuldeep SinghPosted May 17, 2019, 6:46 AM EDT
Nicely explained with great example....thank you for sharing this to all of us.
Haresh PatelPosted Nov 14, 2018, 4:41 PM EST
Thanks Yogendra :)
Nitesh ChauhanPosted Oct 25, 2018, 6:42 PM EDT
Thanks Yogendra
Raj RangaPosted Aug 21, 2018, 1:15 AM EDT
Clean examples!Clean examples!
Amol KumbhkarnaPosted May 19, 2018, 7:47 PM EDT
Great article... Thanks for sharing..!!!!
Виталий ЗаярнийPosted Jan 22, 2018, 10:37 PM EST
Nice manual, thx u
Dennis ThomasPosted Jan 15, 2018, 10:54 PM EST
Very good one............................
Hiten PandyaPosted Jan 12, 2018, 5:39 AM EST
Very straight and simple explanation.
Abhishek GuptaPosted Jan 3, 2018, 10:03 PM EST
Good explanation, It is really helpful
Ram GuptaPosted Jan 3, 2018, 4:57 AM EST
Thanks its helped me
Shyamsunder KashyapPosted Dec 17, 2017, 11:52 PM EST
Nice explanation ...
Raj KumarPosted Jul 30, 2017, 11:33 PM EDT
Nice explanation, It is really helpful
DevX GPosted Jun 12, 2017, 6:28 PM EDT
To the point and understandable.
Santosh RelekarPosted Jun 10, 2017, 11:40 PM EDT
Very nice article , easy to understand.Really helpful
Leo GurdianPosted May 5, 2017, 1:47 PM EDT
Nice great down. Easy to understand. Thanks!
Aishwarya DPosted Mar 16, 2017, 2:32 AM EDT
Nice Article. thanks for sharing
Sijo JoyPosted Nov 4, 2016, 6:49 AM EDT
Good presentation, quite helpful to understand.
siva bPosted Aug 26, 2016, 6:28 AM EDT
Good one .
SubashPosted Aug 20, 2016, 9:42 PM EDT
Nice Share
Prakash KaruppiahPosted Mar 8, 2016, 3:27 AM EST
nice explanation
Wajeed AliPosted Feb 3, 2016, 9:28 PM EST
Thanks good One! helped me...
Sridhar SharmaPosted Jan 3, 2016, 12:03 AM EST
Very nice explanation
Nirmal RoboPosted Dec 30, 2015, 8:40 PM EST
Nice Explanation.
Upendra Pratap ShahiPosted Sep 13, 2015, 10:14 PM EDT
nice one
Advancesharp.comPosted Apr 7, 2015, 4:52 PM EDT
Constant value is initialize at compile timeReadonly at run time Static field in a class serve for all the instances of the class, if first instance change the value of it, all the other instances will get this modified value. For more detail see: http://www.advancesharp.com/blog/1143/constant-readonly-and-static-in-c-with-real-life-examples
Riddhi ValechaPosted Jan 22, 2015, 6:45 PM EST
Hi... I want to ask a query for constant and static variables. Question - In any application, we write the connection string in web config file. Now, when we call this connection string, this should be constant or static ?? From System.Configuration class ??
Shyam SharmaPosted Nov 14, 2014, 4:53 AM EST
but I think there will be a difference while calling a "public readonly" and public static readonly"
vivekPosted Nov 7, 2014, 4:32 AM EST
Thanks , Very explanatory.
Casper BarchagerPosted Sep 23, 2014, 3:48 AM EDT
Aren't the second example wrong? - You're creating two instances of the same class. Var and objVar. That means that Var.re and objVar.re aren't the same variable. So you are not changing the same value? - You're only assigning a value for re once in each instance. Not twice.
kondrasu venkatPosted Sep 10, 2014, 12:42 PM EDT
Thank you a lot for your time and patience. Explained very nicely. Every one in the world can understand this.
Amit Kumar HaldarPosted Jul 2, 2014, 6:03 AM EDT
Very Helpful.. Thanks !
Atul RawatPosted Apr 20, 2014, 7:17 PM EDT
thnx bhaiya nice explanation
bhavani talatamPosted Mar 2, 2014, 9:35 PM EST
very nice article moreover very useful :)
Karthikeyan DPosted Jan 27, 2014, 8:38 PM EST
Really a very good explanation thanks ....
kaushal mani raikwarPosted Jan 9, 2014, 1:44 AM EST
Thanks.... u have explain usefull info in very simple way...
Vishvajeet PawarPosted Dec 31, 2013, 8:52 PM EST
Nice ...Very Imp Article..thanks
lalit pantPosted Jul 26, 2013, 5:20 AM EDT
Nice... very useful.. Thanks