The main difference between constant and readonly is that a constant is evaluated at the compile time while a readonly is evaluated at the runtime.
C# is a modern and general purpose programming language that supports object-oriented programming. It is designed for Common Language Infrastructure (CLI). A variable is a name that is given to a memory location. It can have a value and this value can be used in the program. The variables can be of various types. int variables hold integer values while double variables hold double values etc. These variables can also be declared as constants and readonly. Constant is used for absolute constants while readonly is used for non-absolute constants.
Key Areas Covered
1. What is Constant
– Definition, Functionality
2. What is Readonly
– Definition, Functionality
3. What is the Difference Between Constant and Readonly
– Comparison of Key Differences
C#, Constant, Readonly
What is Constant
Constant is used for absolute constants. The value is set during the declaration of the variable.
One example is as follows.
int const number = 50;
The value of number is set to 50 at the time of declaration. After assigning, it is not possible to change the value of the variable.
The const only allow constants to be used in expressions. One such example is as follows.
int const num1 = 10;
int const num2 = 20;
int const num3 = num1 + num2;
If there is are statements as follows, there will be a compile time error.
int const a =10;
int b =5;
int const c = a+b;
This will give a compile time error as b is non constant.
What is Readonly
Readonly is evaluated at runtime. It is not necessary to set the value at the time of declaration. The value is assigned in the constructor. One example is as follows.
readonly double pi;
pi = 3.14;
// pi = 3.1;
The value pi is assigned inside the constructor. The changeValue() cannot assign a value to pi. Instead, the programmer can initialize the variable at the time of declaration as follows.
readonly double pi = 3.14;
Moreover, readonly can only be declared at class level, not inside methods.
Difference Between Constant and Readonly
Constant refers to a variable that is unchangeable in C# programming while readonly is a keyword in C# that indicates that assignment to the field can only occur as a part of the declaration or in a constructor in the same class.
The main difference between constant and readonly is that while constant is evaluated at compile time, readonly is evaluated at runtime.
The ‘const’ keyword is used for constants while the ‘readonly’ keyword is used for readonly.
Further difference between constant and readonly is that, in constants, it is mandatory to assign values at the time of declaration. But, in readonly, it is not mandatory to assign values at the time of declaration. A value can be assigned in the declaration or in the class constructor.
Moreover, the constants can be declared at the class level and method level. The readonly can be declared only at class level.
Also, while const is used for absolute values, readonly is used for non-absolute constants. This is another difference between constant and readonly.
The constant and readonly may appear similar but they have a difference. The difference between constant and readonly is that a constant is evaluated at the compile time while a readonly is evaluated at the runtime.
1. “What Is Const? – Definition from WhatIs.com.” TheServerSide.com, Available here.