C# Readonly Property

  Keywords : c# readonly example, c# readonly vs const, c# static readonly, c# const and readonly, c# readonly variable, c# readonly list, c# readonly class, c# readonly static variable

Here we will learn what is readonly property in c# and how to use c# readonly property to set field value during declaration or at runtime with examples

C# Readonly Property

In c#, readonly is a keyword which is used to define a read only fields in our applications.  The read only field values needs to be initialized either at the declaration or in a constructor of the same class unlike constant keyword in c#. If we use readonly keyword with fields, then those field values will be evaluated at the runtime.

 

To define a 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 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 a read only field values either at the declaration or in a constructor otherwise we will get a compile time errors in our c# application.

C# Readonly Keyword Syntax

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

 

readonly data_type field_name = "value";

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

 

Following are the different ways of declaring and initializing a read only fields in 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 above example, we created a read only fields with different data types and initializing field values during the declaration and in a constructor.

 

In case if we uncomment the commented code in SetDetails() method, then we will get a 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created a read only fields with different data types and if we uncomment the commented code, then 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 declare and initialized, then that field value must be same throughout the application.

 

When we run above c# program, we will get the result like as shown below.

 

C# Readonly Property Example Result

C# ReadOnly Property Features

Following are the important features of read only variable in 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 assigned to a read only fields, then those values must be a same throughout the application.

C# Difference between Constant and ReadOnly

Following are the difference between constant and readonly properties in 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 time of 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 fields 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.