For any of us who is a software developer, the only thing we are 100% during our journey writing code and delivering software is that we always have something to learn, even we have many years of experience in C# or any programming language. This learning process includes sometimes basic and fundamental concepts, not advanced things.
Given that, I would like to share in this post something related to the difference between readonly and const keywords in C# language. At the first moment, they seem to be the same considering the output is quite similar. However, understand the real differences between them might be important in projects that require the correct use of them in order to not break anything.
The const keyword is typically used in C# language when there is an intention to have an immutable value across the system. Once a const field is initialized with a specific value, it can’t be changed after that. Considering the const value is defined in compile time, in case there is another part of the application trying to assign a new value to the const field, the compiler will indicate the error and successful build won’t happen, as seen in the following image:
The compiler says that “The left-hand side of an assignment must be a variable, property or indexer”. That means that a const can’t be considered as a variable or a property. It can be initialized only when the const is created. This kind of use is extremely important when a certain policy needs to be addressed and kept across the application and it helps us to have more readable code, making sure that the entire application uses the same value. If we don’t use const for that can be broken or can have a wrong implementation would be allowed in terms of the minimum age for a customer. The next image represents the two approaches: one using const and the other a fixed value.
So, const has some characteristics such as:
It can be initialized only on its creation
The same value is kept for the whole application
Extra attempts in assigning values to the const will be caught in compile time
A constant can be specified inside a method
In the other hand, the readonly keyword can have its value changed, but only in a class constructor, as seen in the following example:
As you can see in the image, the compiler shows an error only for the assignment inside the saveCustomer method, but not in the constructor of the Customer class. Therefore, it is possible to assign a default value for a read-only field when it is created and change its value in the class constructor. This is the only situation where a value can be changed for read-only fields after its creation.
Another important aspect is the possibility of specifying different values changing the value in the constructor. Therefore, it is possible to have a different value for the read-only field for each instance of the customer class.
In many situations, depending on business requirements, mutable value for read-only fields may be an interesting idea in case the class needs to assume different values each time in certain conditions. The field will be protected against changes at least in the scope of the class instance.
So, readonly has some characteristics such as:
it is a runtime constant, which means that the application won’t assume a unique value for the entire application
Different from constants, it can be defined inside a method
It is possible to change the value only in the class constructor
It is possible to use it with a static modifier, which is not possible for constants.
The Tech Platform