What is the Difference Between Constant and Readonly

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

Key Terms

C#, Constant, Readonly

Difference Between Constant and Readonly - Comparison Summary

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.

What is the Difference Between Constant and Readonly

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.

class Program{

readonly double pi;

Program(){

pi = 3.14;

}

void changeValue(){

//     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

Definition

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.

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

Assigning Values

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.  

Declaring Level

Moreover, the constants can be declared at the class level and method level. The readonly can be declared only at class level.

Usage

Also, while const is used for absolute values, readonly is used for non-absolute constants. This is another difference between constant and readonly.

Conclusion

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.

Reference:

1. “What Is Const? – Definition from WhatIs.com.” TheServerSide.com, Available here.

Image Courtesy:

1. “Logo C Sharp” By Microsoft –  (Public Domain) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply