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, 6:19 PM
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, 7:34 PM
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, 5:42 AM
Explanation is very apt.
jubula samalPosted Mar 29, 2020, 5:20 AM
Beautiful. Appreciate your effort to compose it so nicely.
RahbarKhanPosted Jan 27, 2020, 11:20 AM
I was asked this question in today's interview (:
Vikas RaoPosted Jan 17, 2020, 4:31 PM
Thanks Sir
Akshay DalviPosted Jan 10, 2020, 12:23 AM
Thanks for your efforts !!
Anil GangwarPosted Jun 24, 2019, 1:25 AM
Very Good explained with great examples
Kuldeep SinghPosted May 17, 2019, 10:46 AM
Nicely explained with great example....thank you for sharing this to all of us.
Haresh PatelPosted Nov 14, 2018, 9:41 PM
Thanks Yogendra :)
Nitesh ChauhanPosted Oct 25, 2018, 10:42 PM
Thanks Yogendra
Raj RangaPosted Aug 21, 2018, 5:15 AM
Clean examples!Clean examples!
Amol KumbhkarnaPosted May 19, 2018, 11:47 PM
Great article... Thanks for sharing..!!!!
Виталий ЗаярнийPosted Jan 23, 2018, 3:37 AM
Nice manual, thx u
Dennis ThomasPosted Jan 16, 2018, 3:54 AM
Very good one............................
Hiten PandyaPosted Jan 12, 2018, 10:39 AM
Very straight and simple explanation.
Abhishek GuptaPosted Jan 4, 2018, 3:03 AM
Good explanation, It is really helpful
Ram GuptaPosted Jan 3, 2018, 9:57 AM
Thanks its helped me
Shyamsunder KashyapPosted Dec 18, 2017, 4:52 AM
Nice explanation ...
Raj KumarPosted Jul 31, 2017, 3:33 AM
Nice explanation, It is really helpful
DevX GPosted Jun 12, 2017, 10:28 PM
To the point and understandable.
Santosh RelekarPosted Jun 11, 2017, 3:40 AM
Very nice article , easy to understand.Really helpful
Leo GurdianPosted May 5, 2017, 5:47 PM
Nice great down. Easy to understand. Thanks!
Aishwarya DPosted Mar 16, 2017, 6:32 AM
Nice Article. thanks for sharing
Sijo JoyPosted Nov 4, 2016, 10:49 AM
Good presentation, quite helpful to understand.
siva bPosted Aug 26, 2016, 10:28 AM
Good one .
SubashPosted Aug 21, 2016, 1:42 AM
Nice Share
Prakash KaruppiahPosted Mar 8, 2016, 8:27 AM
nice explanation
Wajeed AliPosted Feb 4, 2016, 2:28 AM
Thanks good One! helped me...
Sridhar SharmaPosted Jan 3, 2016, 5:03 AM
Very nice explanation
Nirmal RoboPosted Dec 31, 2015, 1:40 AM
Nice Explanation.
Upendra Pratap ShahiPosted Sep 14, 2015, 2:14 AM
nice one
Advancesharp.comPosted Apr 7, 2015, 8:52 PM
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, 11:45 PM
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, 9:53 AM
but I think there will be a difference while calling a "public readonly" and public static readonly"
vivekPosted Nov 7, 2014, 9:32 AM
Thanks , Very explanatory.
Casper BarchagerPosted Sep 23, 2014, 7:48 AM
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, 4:42 PM
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, 10:03 AM
Very Helpful.. Thanks !
Atul RawatPosted Apr 20, 2014, 11:17 PM
thnx bhaiya nice explanation
bhavani talatamPosted Mar 3, 2014, 2:35 AM
very nice article moreover very useful :)
Karthikeyan DPosted Jan 28, 2014, 1:38 AM
Really a very good explanation thanks ....
kaushal mani raikwarPosted Jan 9, 2014, 6:44 AM
Thanks.... u have explain usefull info in very simple way...
Vishvajeet PawarPosted Jan 1, 2014, 1:52 AM
Nice ...Very Imp Article..thanks
lalit pantPosted Jul 26, 2013, 9:20 AM
Nice... very useful.. Thanks