What is the difference between constant and read-only?
The main difference between constant and readonly is that a constant is evaluated at compile time, whereas a readonly read is evaluated at runtime.
C# is a modern, general-purpose programming language that supports object-oriented programming. It is designed for the Common Language Infrastructure (CLI). A variable is a name given to a memory location. It can have a value and this value can be used in the program. Variables can be of various types. int variables contain integer values, while double variables contain double values, etc. These variables can also be declared as constants and read-only. 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 read-only?
– Comparison of key differences
Key terms
C#, constant, readonly
what is constant
Constant is used for absolute constants. The value is set during the declaration of the variable.
An example is the following:
int const number = 50;
The value of the number is set to 50 at declaration time. After assignment, it is not possible to change the value of the variable.
Constants only allow the use of constants in expressions. An example is the following.
int const num1 = 10;
int const num2 = 20;
int const num3 = num1 + num2;
If there are statements like the following, 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 since b is not a constant.
What is ReadOnly?
Readonly is evaluated at run time. It is not necessary to set the value at declaration time. The value is assigned in the constructor. An example is the following.
class program
read only double pi;
Program()
pi = 3.14;
void changeValue()
// pi = 3.1;
The pi value is assigned inside the constructor. The changeValue() cannot assign a value to pi. Instead, the programmer can initialize the variable at declaration time as follows:
double read only pi = 3.14;
Also, it can only be declared at the class level, not inside methods.
Difference between constant and readonly
Definition
Constant refers to a variable that cannot be changed in C# programming, while readonly is a keyword in C# indicating that the assignment to the field can only occur as part of the declaration or in a constructor in the declaration. class.
Evaluation
The main difference between constant and readonly is that while constant is evaluated at compile time, readonly is evaluated at runtime.
Keywords
The ‘const’ keyword is used for constants while the ‘readonly’ keyword is used for read only.
Assigning values
The additional difference between constant and readonly is that, for constants, it is mandatory to assign values at declaration time. But, in the read-only case, it is not mandatory to assign values at declaration time. A value can be assigned in the declaration or in the constructor of the class.
declaration level
Also, constants can be declared at the class level and method level. Read can only be declared at the class level.
Use
Also, while const is used for absolute values, readonly is used for non-absolute constants. This is another difference between constant and read-only.
conclusion
Constant and read-only may look similar but they have a difference. The difference between constant and readonly is that a constant is evaluated at compile time, while a readonly is evaluated at run time.
Reference:
1.”What is Const? – Definition from WhatIs.com.” TheServerSide.com , Available here.
Courtesy image:
1. “C Sharp logo” By Microsoft – (Public Domain) via Commons Wikimedia