Const refers to a constant variable and the value of which need to be assigned only once, during declaration. When the keyword const is used during a declaration, it becomes a constant meaning the value remains constant and cannot be changed throughout the program. The constant values are also called as literals. It can be of any basic data type such as an integer const, a floating const, or a string literal.
The Readonly keyword is a modifier that can be used only on fields but not local variables. A readonly field can be initialized either at the time of declaration or inside a constructor in the same class, so the values can be different for different executions of the program depending on the constructor used. The readonly keyword specifies that an instance variable of an object is not modifiable and any attempt to modify it after declaration results in compilation error.
Constant const keyword is nothing but immutable variables the value of which can only be initialized at the compile time. These constant values are also called as literals. It simply allows us to specify whether a variable is modifiable or not. In simple terms, constants are implicitly static and they should be initialized before they are to be used in the program. Readonly keyword, on the other hand, is a modifier that can be used on fields and the value of which can be changed during runtime.
It prevents fields from being changed and the values of which can be set through reference variables. A const is static by default and the values for which should be assigned at the time of variable declaration meaning the values are assigned only at compile time.
The initialize of a constant field must be a constant expression and may not be modified. Simply put, a const is a compile-time constant. A readonly field, on the other hand, can only be initialized at the class level not inside the method and the field cannot be modified after initialization.
In other words, readonly variables can be initialized at the time of declaration or inside a constructor. A const value is put directly into the code as it acts as the compile-time replacement. The compiler places the constant value in the assembly metadata that defines the constant and embeds the value in the IL code itself after initialization, so no memory is required for constants at runtime for allocation.
The readonly fields are stored in the dynamic memory so the values can only be obtained at runtime. Although both the keywords are used almost for the same purpose, they have different binary versioning semantics. When an expression references a constant, the compiler embeds the value at compile-time. So if the value is used in a different assembly, the original assembly needs to be updated and value should be changed. Now try to change the value in the default constructor.
Now in the snapshot above you can see it's built successfully without an error, warning or messages. Let's check if there is a runtime error. Now here we can see that there is not a runtime error and the value was assigned successfully to the Readonly variable. Now one gotcha is, now 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, and passing a value as "Hello Frend'z" and 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!!
And the value can be changed again and again through a constructor. Now move ahead to Static Readonly variables. Static ReadOnly 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. It can change only once at runtime.
Let's understand it practically. It can be declared inside the method. In readonly fields, we can assign values in declaration and in the constructor part. In const fields, we can only assign values in declaration part.
It can be used with static modifiers. It cannot be used with static modifiers. Skip to content. Change Language. Related Articles.
Table of Contents. Save Article. Improve Article. Like Article.
0コメント