C# Readonly Property

In c#, readonly is a keyword, and it is useful to define read-only fields in our applications. The read-only field values need to be initialized either at the declaration or in a constructor of the same class, unlike the constant keyword in c#. If we use readonly keyword with fields, those field values will evaluate at the runtime.

 

To define read-only fields in c#, we need to use readonly keyword during the declaration of fields in our application, and we can use readonly modifier with the numbers, boolean values, strings, or null references.

 

In c#, if we use readonly keyword to define the read-only field, that field value cannot change once the constructor execution has finished, so we should not use readonly keyword with the fields whose value will change at any time.

 

It’s mandatory to initialize read-only field values either at the declaration or in a constructor; otherwise, we will get compile-time errors in our c# application.

C# Readonly Keyword Syntax

Following is the syntax of defining read-only fields using readonly keyword in c# programming language.

 

readonly data_type field_name = "value";

If you observe the above syntax, we used a readonly keyword to declare a read-only variable in our application.

 

The following are the different ways of declaring and initializing read-only fields in the c# programming language.

 

class User {
    // Initialize Read Only Fields
    public readonly string name="Suresh Dasari";
    public readonly string location;
    public readonly int age;
    public User() {
       location = "Hyderabad";
       age = 32;
    }
    public void SetDetails() {
       // Compile error if uncommented
       //location = "Guntur";
       //age = 30;
    }
}

The above example shows that we created read-only fields with different data types and initialized field values during a constructor's declaration.

 

Suppose, if we uncomment the commented code in SetDetails() method, we will get compile errors because, in c#, we can initialize the read-only field values during declaration or in a constructor.

C# Readonly Property Example

Following is the example of defining and using read-only fields in c# programming language with readonly keyword.

 

using System;

namespace Tutlane
{
    class User
    {
       // Initialize Read Only Fields
       public readonly string name = "Suresh Dasari";
       public readonly string location;
       public readonly int age;
       public User()
       {
          location = "Hyderabad";
          age = 32;
       }
    }

    class Program
    {
       static void Main(string[] args)
       {
           User u = new User();
           // This will throw compile time error
           //u.name = "Rohini Alavala";
           Console.WriteLine("Name: {0}", u.name);
           Console.WriteLine("Location: {0}", u.location);
           Console.WriteLine("Age: {0}", u.age);
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we created read-only fields with different data types, and if we uncomment the commented code, we will get a compile-time error because we are trying to change the value of read-only fields.

 

As discussed, once the read-only field is declared and initialized, that field value must be the same throughout the application.

 

When you run the above c# program, you will get the result below.

 

C# Readonly Property Example Result

C# ReadOnly Property Features

The following are the important features of the read-only variables in the c# programming language.

 

  • In c#, we can create the Read-only fields using readonly keyword.
  • In c#, you can initialize the readonly fields either at the declaration or in a constructor.
  • The readonly field values will evaluate during the run time in c#.
  • Once values assign to the read-only fields, those values must be the same throughout the application.

C# Difference between Constant and ReadOnly

The following are the difference between constant and readonly properties in the c# programming language.

 

  • In c#, you can create the constant fields using const keyword and the read-only fields created by using readonly keyword.
  • In c#, the constant fields are initialized during the declaration, but the read-only fields are initialized either at the declaration or in a constructor.
  • The constant field values are evaluated during the compile-time, but the read-only field values are evaluated at run time in c#.

This is how we can create and use read-only fields in our c# programming language with readonly keyword based on our requirements.