Difference Between Readolny And Const Keyword In C

SHARE

Keywords are of the following types:

  1. Compile time Keywords (const)
  2. Runtime Keywords  (Readonly)

What is Readonly?

Read-only keyword is a designation for any object or construct which can no longer be altered after creation, it can only be read. This can refer to both hardware and software constructs such as read-only memory chips like BIOS and CMOS and CD/DVD/Blu-ray-ROMs for hardware, and read-only files for software constructs. In either case the contents of the object when marked as read-only can no longer be changed, only accessed or read.

In software, read-only is a safety measure which protects files and data from accidental or intentional alteration or deletion and may be imposed only for select users or groups of users. This means that some users see the files as read-only while others are allowed to alter or delete it; this is widely used for permissions and security purposes.

What you need to know about readonly

  • Readonly keyword is used to create a readonly fields
  • Readonly is a constant defined at runtime.
  • Readonly field value can be changed after declaration.
  • Readonly fields cannot be defined within a method.
  • Readonly variables are declared as instance variable and assigned values in constructor. Therefore, readonly variables are used for the run-time constants.
  • It can be used with static modifiers.

What is Const?

The const keyword is used in making a variable constant, i.e indicating that its value cannot be changed once you instantiate it.The idea is to make sure that we don’t accidentally change the values of variables that need to be constant throughout the program. Assign it to those variables which are frequently used in the program and retain the value that is assigned first.These variables must be instantiated once they are declared.

In other words, the const keyword allows a programmer to tell the compiler that a particular variable should not be modified after the initial assignment in its declaration. If any code tries to assign a new value to that variable, the compiler will generate an error “Assignment to read-only variable not allowed” indicating that the assignment operation should not occur. This allows a programmer to prevent unwanted modifications to variables that for some reason should not be changed. Note that const variables must be initialized; otherwise there is no other way to assign them values.

What you need to know about Const

  • Const keyword is used to create constant fields. By default, a const is static that cannot be changed.
  • Const is used to create a constant at compile time.
  • Const field value cannot be changed after declaration.
  • Const fields can be declared within a method.
  • Const fields are to be assigned at the time of declaration. Therefore, const variables are used for compile-time constants.
  • It cannot be used with static modifiers.

Difference Between Readonly And Const Keyword In Tabular Form

BASIS OF COMPARISON   READONLY CONST
Description Readonly keyword is used to create a readonly fields.   Const keyword is used to create constant fields.
Use Readonly is a constant defined at runtime.   Const is used to create a constant at compile time.  
Field Value After Declaration Readonly field value can be changed after declaration.   Const field value cannot be changed after declaration.  
Declaration with a Method Readonly fields cannot be defined within a method.   Const fields can be declared within a method.  
Variables Readonly variables are declared as instance variable and assigned values in constructor. Const fields are to be assigned at the time of declaration.
Static Modifiers It can be used with static modifiers.   It cannot be used with static modifiers.