Const And Readonly In Csharp
The immutable values that were known at the time of programme compilation and remain unchanged during the duration of the programme are known as constant variables.
Although the values of the read-only variables can't be changed once they are known at runtime, they are also immutable.
Constant Variables in C#:
A variable in C# is considered a constant if the const keyword is used to declare it, and a constant variable's value cannot be changed once it has been declared. Therefore, initialising the constant variable is required only at the moment of declaration.
Let's say your software calls for the declaration of a constant PI. Once that is done, declare the constant variable as follows:
const float pi=3.14f;
Important point to be remembered while declaration of const variables:
- There will be only one creation of the constant variables. This is the case because, once defined, the constant values are immutable.
- If it enables the creation of many copies of the constant variable, all those copies will have the same data, resulting in a memory waste. Therefore, it is a waste of resources when we cannot change a value and when we create the same copy more than once.
- Constant variables behave similarly to static variables in that they are initialised only once over the life of a class and do not need an instance of the class to be initialised or used.
- In order to construct a "constant" variable, the term const is used. It implies that a variable will be created whose value will never change. In layman's terms, a constant variable is one whose value cannot be altered or changed more than once after its declaration.
- Constants are static by default.
- When a constant variable is declared, it needs to be initialised.
- A constant variable behaves in the same way as a static variable, meaning that it only keeps one copy during the class's lifetime and initializes right away once class execution begins (an object is not necessary).
EXAMPLE:
using System;
namespace ConstDemo
{
class Constant
{
//At the moment the const variable is declared,
//we must give it a value
//a compile-time error will occur.
const float pi = 3.14f;
static void Main(string[] args)
{
Console.WriteLine(Constant.pi);
//Within the same class, we can also directly access them.
Console.WriteLine(pi);
//A constant variable may also be declared inside a function
const int n = 4;
Console.WriteLine(n);
//A constant variable's value cannot be changed after it has been declared. Thus, the line below has an error.
Console.ReadLine();
}
}
}
OUTPUT:
![Const And Readonly In C#](https://static.tutorialandexample.com/csharp/const-and-readonly-in-csharp1.png)
Readonly Variables in C#:
A read-only variable is one that has the readonly keyword used in its declaration and cannot be changed after initialization like a constant.
So, initialising a read-only variable is not required at the moment of declaration; it can also be done in the constructor. Therefore, we are only able to alter the value of the read-only variable within a constructor.
Since they are initialised just once for each instance of the class and only after the class is constructed, read-only variables function similarly to non-static variables in C#.
We can therefore think of it as a non-static variable, and an instance is required in order to access read-only variables.
Important points to be remembered while declaration of Readonly variables:
- In C#, a read-only variable is a variable that was created with the readonly keyword. Once initialised, the read-only variable's value cannot be changed.
- Similar to a constant, initialising a read-only variable is neither necessary or required at the moment of declaration. The read-only variables can be initialised inside of a constructor, but it's crucial to remember that once initialised, they cannot have their values changed outside of the constructor.
- A read-only variable behaves similarly to a non-static variable in terms of behaviour. It does this by keeping a different copy for each object.
- The read-only variable's value cannot be altered from outside the constructor body, whereas the non-static variable's value can, and that is the only difference between these two.
Example:
using System;
namespace ReadOnlyDemo
{
class Readonly
{
readonly int n = 10;
//Additionally, you can initialise the constructor.
public Readonly()
{
n = 4;
}
static void Main(string[] args)
{
Readonly obj = new Readonly();
Console.WriteLine(obj.n);
//The next sentence will result in a compile-time error.
//obj.number=4
Console.ReadLine();
}
}
}
Output:
![Const And Readonly In C#](https://static.tutorialandexample.com/csharp/const-and-readonly-in-csharp2.png)
Explanation:
The read-only variable in the example above is initialised using the class constructors. The read-only variables can either be initialised directly at the time of declaration or indirectly through class constructors. In C#, read-only variables cannot have their value changed once it has been initialised.
Difference between Const and Read-only in C#:
Const | Readonly |
Used for only const fields of creation | Used for only Readonly fields of creation |
Compile-time usage | Run-time usage |
Values cand be altered | Values cannot be altered |
Possible within the method | Not possible within the method |
Compatible with static methods | Compatible with non-static methods |