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
- 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 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.