Posts

Showing posts with the label ReadOnly and Static ReadOnly

C# | Difference Between Const, ReadOnly and Static ReadOnly

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