Difference Between Similar Terms and Objects

Difference Between Constant and ReadOnly

It is important to understand the concepts of C# programming before you proceed with implementing the code. Before we jump to the differences between const and readonly, let’s understand the keywords first and how they are used in a program. Both are the most common keywords used in C# programming and basically for the same purpose. However, both the keywords are very different from each other. Const is a reserved word used to represent constant values and it specifies that a value cannot be modified after compile-time, whereas readonly is a modifier that can be used on fields and the value of which can be changed during runtime. Let’s take a look at some of the key differences between const and readonly.

What is a Const?

Const refers to a constant variable and the value of which need to be assigned only once, during declaration. When the keyword const is used during a declaration, it becomes a constant meaning the value remains constant and cannot be changed throughout the program. It’s more of a reserved word which specifies that a value must not be modified after compile-time. A const is implicitly static by default, so it can be called with the class name using “Classname.VariableName”. The constant values are also called as literals. It can be of any basic data type such as an integer const, a floating const, or a string literal.

What is ReadOnly?

The Readonly keyword is a modifier that can be used only on fields but not local variables. A readonly field can be initialized either at the time of declaration or inside a constructor in the same class, so the values can be different for different executions of the program depending on the constructor used. The readonly keyword specifies that an instance variable of an object is not modifiable and any attempt to modify it after declaration results in compilation error. The variable doesn’t become non-modifiable until after the execution. Variables are made readonly only to stop calling the code from accidentally modifying it after it’s constructed.

 

Difference between Constant and ReadOnly

  1. Basic

Constant (const) keyword is nothing but immutable variables the value of which can only be initialized at the compile time. These constant values are also called as literals. It simply allows us to specify whether a variable is modifiable or not. In simple terms, constants are implicitly static and they should be initialized before they are to be used in the program. Readonly keyword, on the other hand, is a modifier that can be used on fields and the value of which can be changed during runtime. It prevents fields from being changed and the values of which can be set through reference variables.

  1. Initialization

A const is static by default and the values for which should be assigned at the time of variable declaration meaning the values are assigned only at compile time. A constant is declared using the “const” keyword. The initialize of a constant field must be a constant expression and may not be modified. Simply put, a const is a compile-time constant. A readonly field, on the other hand, can only be initialized at the class level not inside the method and the field cannot be modified after initialization. In other words, readonly variables can be initialized at the time of declaration or inside a constructor.

  1. Memory Allocation

A const value is put directly into the code as it acts as the compile-time replacement.  The compiler places the constant value in the assembly metadata that defines the constant and embeds the value in the IL code itself after initialization, so no memory is required for constants at runtime for allocation. Readonly value is not constant and it’s stored in the loader heap the memory for which cannot be allocated until its type is loaded. The readonly fields are stored in the dynamic memory so the values can only be obtained at runtime.

  1. Binary Versioning

Although both the keywords are used almost for the same purpose, they have different binary versioning semantics. When an expression references a constant, the compiler embeds the value at compile-time. So if the value is used in a different assembly, the original assembly needs to be updated and value should be changed. The changes in the assembly are not reflected until it’s recompiled. The value is not obtained until runtime when an expression references a readonly field. If the value is changed in a different assembly, the changes get reflected in the assembly as soon as it’s loaded without needing to recompile it.

Const vs. Readonly: Comparison Chart

Summary of Const vs. Readonly

Both the keywords are used for the same thing that is to declare a member so that the values cannot be changes throughout the program. However, const keyword is very different from the readonly keyword. Const is a constant variable the value of which should be initialized at the compile-time, whereas a readonly field can only be initialized at the class level, either at the time of declaration or inside the constructor. Const is a compile-time constant whereas readonly fields are read-only. Constance variables can be accessed using “Classname.VariableName” while readonly variables can be accessed using “InstanceName.VariableName”. Moreover, const variables can be declared in methods, whereas readonly fields cannot be declared in methods.

 

Latest posts by Sagar Khillar (see all)

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


2 Comments

  1. Nyc information

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Hejlsberg, Anders, et al. The C# Programming Language. San Francisco: Adobe Press, 2006. Print

[1]Doyle, Barbara. C# Programming: From Problem Analysis to Program Design. Boston: Cengage, 2007. Print

[2]Turtschi, Adrian, et al. C#.Net Developer's Guide. New York City: Elsevier, 2002. Print

[3]Image credit: https://upload.wikimedia.org/wikipedia/commons/e/ed/Content-block-msharp.png

[4]Image credit: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Bandgap_vs_Lattice_Const_for_Semiconductors_5.25-6.74_Angstrom.jpg/588px-Bandgap_vs_Lattice_Const_for_Semiconductors_5.25-6.74_Angstrom.jpg

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder