Introduction
C# is pronounced as C sharp. This is built by Microsoft and operates on the.Net framework. It is closely related to the C programming language, as well as other computer languages such as C++ and Java. Its most impressive feature is its reusability. The language is easy to learn and understand. Keywords are the reserved words that have a special meaning for the compiler. These terms cannot be used as identifiers for variables, classes, or anything else.
Constant fields and constant local are declared with the const keyword. The constant field's value remains constant throughout the program; in other words, once the constant field is assigned, its value cannot be modified.This readonly keyword indicates that the variable can only be assigned when it is declared in a variable or the constructor of the same class in which it is declared.
So first, let us discuss more about readonly and const keyword.
Click on the following link to read further: Features of C Language
readonly keyword
readonly is a keyword in C# that may be used to create read-only fields in our applications. Unlike the constant keyword in C#, the read-only field values must be populated either at the declaration in a function or constructor of the same class. When we employ the readonly keyword with fields, the values of those fields will be evaluated at runtime. We must use the readonly keyword during the declaration of fields in our application to define read-only fields. We can use the readonly modifier with numbers, boolean values, strings, or null references. If we use the readonly keyword to specify a read-only field in C#, the value of that field cannot change once the execution of the constructor has finished. Hence we shouldn't use readonly with fields whose value will change at any moment.
The following are the main properties of the readonly keyword:
- When an instance is created, they are assessed.
- For readonly fields, you can use the static modifier.
- With reference types, the readonly modifier can be utilised.
- The readonly keyword can only be used for instance or static fields; it cannot be used for variables in methods.
Consider the following code snippet:
using System;
class ReadOnlyTest
{
//readonly variable are initilized during declaration or in a constructor
readonly int RollNo = 1284;
//Valid
readonly int marks;
readonly string Name = "Ninja";
//readonly fields can be initlized in constructor
public ReadOnlyTest (string name)
{
marks = 90;
Name = name;
}
public changeName(string newName)
{
Name = "Ninja"; ; // Will result error.
}
}
class ReadOnlyExample
{
public static void Main()
{
ReadOnlyTest obj = new ReadOnlyTest("Ninja");
Console.Read();
}
}
Explanation:
In the above code snippet, we have tried to reassign and increment the values of readonly variables, which results in errors.
Example:
Consider the following code snippet:
using System.IO;
using System;
public class Result {
public const int marks = 50;
public readonly int marks_1;
Result(int marks){
marks_1 = marks;
}
public static void Main() {
Console.WriteLine(marks);
Result p1 = new Result(61);
Console.WriteLine(p1.marks_1);
}
}
Output :
50
61