Difference Between Const, ReadOnly and Static ReadOnly in C#

The cost, 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 cost 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.

Variables

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

const type

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

 Const field

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.

Readonly in C#

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.

Static constructor

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.

 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.

Runtime error

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.

Readonly variable

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.

String

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

Static ReadOnly in C#

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.

Compile time error

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 cost vs. read-only vs. static read-only in C#. You also learned when to use these variables in your program.


Similar Articles