This article explains the three most commonly used but confusing keywords. You can use these keywords in your code correspondingly but what you need to learn about these keywords is, how they are function differently, their behaviour and their different operation. I am explaning with simple snippets by taking reference of C#.
Agenda
- Overview
- Constant
- Snippet
- Explanation
- Points to Remember
- Readonly
- Snippet
- Explanation
- Points to Remember
- Static
- Snippet
- Explanation
- Points to Remember
- Conclusion
Overview
Constant, readonly and static are keywords that are nearly the same in behavior but when we talk about their functioning they are all different. I'll explain all three wirth some examples, codes and their respective examples.
This article will also include some key points that you need to remember, so that you can easily differentiate among these three.
Constant
Constant fields are defined at the time of declaration in the code snippet, because once they are defined they can't be modified. By default a constant is static, so you can't define them static from your side.
It is also mandatory to assign a value to them at the time of declaration otherwise it will give an error during compilation of the program snippet. That's why it is also called a compile-time constant.
Snippet
- void Sum(int j)
- {
- const int i = 9, k = 2;
- const int A = i + k;
- }
Output: 11
Explanation
The preceding code snippet will produce a result of 11, without showing any error since we already declared it at the initial point of declaration.
What if, we make some changes in the code above, such as:
- void Sum(int j)
- {
- const int i = 9, k = 2;
- // const int A = i + k;
- Const int B = i + j;
- }
Explanation
This code snippet will take you toward a compile-time error, because there is no initialization, since it's evaluated at run time.
Points to Remember
- Compile-time constant
- Can't be declared static
- Can't be modified or changed
- Can be of any type of Access Modifier
- Local scope
- Needs to get initialized
- Declared at the time of declaration
Readonly
A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).
For that reason a Readonly field is also called a run-time constant.
Snippet
- class ReadOnly
- {
- readonly int i;
- public ReadOnly( )
- {
- i = 11;
- Console.WriteLine(i);
- }
- }
Explanation
We can assign the value to an integer later in the snippet; this is possible when using the Readonly keyword. We can modify it too depending on the use.
Points to Remember
- Run-time constant
- It can be static
- Global scope
- Can be declared in the constructer class
- Generally public
Static
The static keyword is used to declare a static member. If we are declare a class as a static class then in this case all the class members must be static too. The static keyword can be used effectively with classes, fields, operators, events, methods and so on effectively.
Snippet
- class ReadOnly
- {
- static int i = 11;
- public static void disp()
- {
- Console.WriteLine(i);
- }
- }
Output: 11
Explanation
This code will show no error and produce a result (11), since we declared its value to be static at the time of declaration. So we can access it depending on our use in the program.
Let's make some changes in the snippet and see what happens then.
- class ReadOnly
- {
- int i = 9;
- public static void disp()
- {
- Console.WriteLine(i);
- }
- }
Explanation
This snippet will show an error, because we didn't declare a value for the static and we are trying to access it within a method. We can't do that.
Points to Remember
- Can't be used with indexers
- Works with constructors too
- By default it is private
- Can be parameterized or public too
- If its applied to a class then all the class members need to be static
Conclusion
I hope you now have 3 different states in your mind regarding the constant, static and readonly keywords and that you will be able to differentiate among them easily.

Praneet RanePosted Aug 30, 2019, 8:35 PM
Very informative, useful and to the point
Bhavesh JadavPosted Oct 1, 2018, 5:18 AM
Before reading it I am very confused to that now I understood what is the difference between it. Very useful to me, thanks for shoring it.
Jayant SatkarPosted May 29, 2017, 9:39 AM
Not too much good, guys just go to MSDN and check it
Hasmukh PrajapatiPosted Jan 11, 2017, 9:44 AM
Very useful, Thanks buddy
Anil SinghPosted Jan 27, 2016, 7:36 AM
It might help you for "What is the difference between constant, readonly and static in C#?" https://lnkd.in/eSReStq
Abhishek JaiswalPosted Dec 8, 2015, 9:11 AM
Thank you Upendra Pratap Shahi and jayesh goyani! :)
jayesh goyaniPosted Nov 27, 2015, 4:36 AM
Nice article.
Upendra Pratap ShahiPosted May 6, 2015, 3:49 AM
informative article..
Former memberPosted Mar 3, 2015, 3:43 PM
"This snippet will show an error, because we didn't declare a value for the static and we are trying to access it within a method. We can't do that."you have not mentioned the correct reason..static methods cannot contain non static instance variables because non static variables are loaded into memory when we create an object of that class, so it would give a compiler error
Abhishek JaiswalPosted Sep 16, 2014, 12:40 AM
Okay, thanks arunava, I'll cloned it out! :)
Arunava BhattacharjeePosted Sep 15, 2014, 11:30 PM
You can check this on MSDN: http://msdn.microsoft.com/en-IN/library/acdd6hb7.aspx . "Readonly" means you can only read the value,can't write it.A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. But you can change the value by reflection,but that is different case. If you see WPF, dependency properties are all readonly. So the property values are determined at run-time "depending" the value precedence. But once a value is assigned only different constructor calls can assign a new value, but that value can't be changed else where. Thus readonly is known as "runtime constant". This is my understanding.Probably you meant the same as you mentioned "constructor", but my problem is with the word "change". We can't change it, your second statement is fine with me which states "or assign a value to it at runtime".First phrase created the confusion. I may be wrong, there is always a scope for new learning. So can you please explain?
Abhishek JaiswalPosted Sep 15, 2014, 10:22 AM
thanks sahil! :)
Arunava BhattacharjeePosted Sep 15, 2014, 3:47 AM
Well I do have a problem with your line which says"We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only)." In my knowledge what I know about readonly is "once the value is assigned, further modification is not at all possible till the lifetime of the application."
Sahil SharmaPosted Sep 14, 2014, 12:13 PM
Informative. Thanks for sharing!
Manju lata YadavPosted Aug 20, 2014, 1:51 AM
nice article.. you can also include why to use which.