C# Readonly Property

In c#, readonly is a keyword that 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, then those field values will be evaluated 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 with null references.

 

In c#, if we use readonly keyword to define the read-only field, then that field value cannot be changed once the constructor execution has finished, so we should not use readonly keyword with the fields whose value will be changed 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;
    }
}

If you observe the above example, we created read-only fields with different data types and initialized field values during a constructor's declaration.

 

If we uncomment the commented code in SetDetails() method, then we will get compile errors because, in c#, the read-only field values can be initialized either 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, then that field value must be the same throughout the application.

 

When you run the above c# program, you will get the result as shown 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.

 

  • Read-only fields in c# can be created by using readonly keyword.
  • In c#, the readonly fields can be initialized either at the declaration or in a constructor.
  • The readonly field values will be evaluated during the run time in c#.
  • Once values are assigned to read-only fields, then 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#, the constant fields can be created by using const keyword and read-only fields can be created by using readonly keyword.
  • In c#, the constant fields can only be initialized during the declaration, but read-only fields can be initialized either at the declaration or in a constructor.
  • Constant field values will be evaluated during the compile-time, but read-only field values will be 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.