c sharp

C# Readonly

In C#, whenever it’s necessary to specify which fields are read-only, we employ the read-only keyword. Read-only fields specified with the keyword read-only are evaluated at runtime; and if a field is specified as read-only then the values of the field cannot be altered, once the constructor in which it is defined has been done. The read-only keyword should not be used with fields whose values may change at any time. The strings, integers, null references, and Boolean values can all be used with the read-only keyword.

What is the read-only in C# in Ubuntu 20.04?

The read-only keyword makes a variable or objects readable only. This signifies that the variable or object can only be assigned a value in the constructor or at the class level. In any method other than the constructor, we cannot update or reassign a read-only variable or an object a value. When the readonly modifier is applied to a value type field, it makes it immutable. The read-only modifier prohibits a reference type field from being replaced by separate instances of the type referencing; however, the readonly modifier does not prevent the read-only field’s instance value from being updated.

Syntax of the reading in C# in Ubuntu 20.04

The readonly keyword’s syntax for defining read-only fields in the c# programming language is as follows:

# Readonly datatype variable = value.

In the code, we have declared a read-only variable with the readonly keyword. The data type is used here for the read-only variables data type property.

How to utilize the readonly modifier in C# in Ubuntu 20.04

To understand the fundamental role of the readonly modifier in the C#, we have some examples of code implementation and execution below:

Example 1: Using the readonly modifier to create read-only fields in C# in Ubuntu 20.04

In the following c# code example, we have declared and initialized read-only fields with the read-only modifier.

We have included the system library of C# as a “using the system” to access the required method and function. The class is created with the name “STR”. In the class, we have three read-only keywords which are kept public. The readonly keyword is used for the variables and the data type of these read-only variables is set as “string”. Then we have generated a constructor of the class “STR” in which the read-only variables have their values assigned to them.

The writeLine method is invoked to print the assigned values of the read-only variables. further, the void main function is defined as static. In the void main function block, we have called the class “STR” and created its object as “obj1”. The string values are initialized for the read-only variables.

The output displays the string values of specified read-only variables respectively in the image below.

Example 2: Using the readonly modifier to create a read-only structure in C# in Ubuntu 20.04

The readonly keyword in a read-only structure indicates that the structure is immutable. When creating a read-only structure, it must apply the readonly modifier with all of its fields; otherwise, the compiler will throw an error.

First, we imported the “using system” library. The read-only structure is also created as a “Book”. In the read-only structure, we have declared read-only variables with different data types. The variable “Title” and “Author” have a String data type and the variable “Year” has the int data type. We have included the getter method for these read-only variables. Then the parameterized constructor is created here for the read-only variables declaration and assigns them a value. In the read-only structure constructor, we have defined the “this” keyword as a variable that we can change only in the constructors but not in any other method.

After that, the class is created as “info” and called the void main function. In the main function, we have invoked the read-only structure “Book” and declared an object to it as “b” and passed the value according to the specified data type. The WriteLine method is used to print the values of these read-only structure variables.

Upon execution of the above C# program, we have the output shown on the Ubuntu console screen.

Example 3: Using the readonly modifier to create read-only members in C# in Ubuntu 20.04

In the read-only member’s functionality, the readonly modifier can be added to any member of the structure. This readonly modifier means that the member can’t change anything. It’s preferable to apply read-only to the entire structure.

The system library of C# is imported as it is necessary for the C# program. The structure is created as an “Employee” and kept public. In the “Employee” block, we have declared the three variables. The first variable is defined as “Name” and sets its data type to string. The second variable is named “Age” which is of integer data type. The last variable is the read-only member named “Dept” which is of string data type. The getter method is invoked for these variables. Then the parameterized constructor is created for assigning the values for the above-declared variables. The defined variables are used with the “this” variable, which cannot be altered in any method other than the constructor.

After that, the class is created as “EMP” and called the main function of the class. In the main function, we have set the values for the variables and read-only members to an object “e1”. These values can be shown through the C# writeLine method.

The output of the read-only member and the other variables is clearly shown on the terminal screen.

Example 4: Using the readonly modifier to create static read-only variables in C# in Ubuntu 20.04

A Static Read-only class variable’s value can be created at execution or compile time, then changed later. Nevertheless, we can only modify the state of this variable in the static function Object() { [native code] }; non-static and static functions cannot alter the value of static read-only variables.

We built a class called “StaticReadOnlyVariable” in the above code. In the class, we have declared the static read-only variables as “str1” and “str2”. These variables are of string data type. Note that the variable “str2” is initialized at run-time declaration. Then the class “StaticReadOnlyVariable” is called here in which we declare the static variable and assign it a string value. The writeLine method printed these variables’ values. The main function is defined and declares the object “s” for the class “StaticReadOnlyVariable”.

The output of the static read-only variable is displayed in the image below.


The article aims to demonstrate to you the use of the read-only keyword in c#. We have clarified the definition of the read-only keyword along with its basic syntax. We have also discussed the working of the read-only keyword in different ways with the various example programs and their outputs. Based on our requirements, this is how we construct and use read-only fields by using the read-only keyword in the C# programming language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.