Readonly in C#

28 Nov, 2019
In C#, a readonly keyword is a modifier which is used in the following ways:
1. Readonly Fields: In C#, you are allowed to declare a field using readonly modifier. It indicates that the assignment to the fields is only the part of the declaration or in a constructor to the same class. Such types of fields can only be assigned or reassigned multiple times only at the declaration or in a constructor. They are not assigned after the constructor exit. If the readonly modifier is used with a value type field, then the field is immutable. And if the readonly modifier is used with a reference type field, then the readonly modifier prevents the field from replaced by the different instances of the reference type, here the readonly modifier does not stop the instance data of the field from being modified through the read-only field. In static and instance constructors, you are allowed to pass a readonly field as an out or ref parameter.
Example:
// C# program to illustrate// how to create a readonly fieldusing System;  class GFG {      // readonly variables    public readonly string str1;    public readonly string str2;      // Readonly variable    // This variable is     // initialized at declaration time    public readonly string str3 = "gfg";      // The values of the readonly    // variables are assigned    // Using constructor    public GFG(string a, string b)    {          str1 = a;        str2 = b;        Console.WriteLine("Display value of string 1 {0}, "                         + "and string 2 {1}", str1, str2);    }      // Main method    static public void Main()    {        GFG ob = new GFG("GeeksforGeeks", "GFG");    }}
Output:
Display value of string 1 GeeksforGeeks, and string 2 GFG
Here, the value of the str1 and str2 is assigned in the constructor.
2. Readonly Structure: In the readonly structure, readonly modifier indicates that the given structure is immutable. When you create a readonly structure, it is necessary to use a readonly modifier with its fields, if you do not do this, then the compiler will give an error.
Example:
// C# program to illustrate how // to create a readonly structureusing System;  // Readonly structurepublic readonly struct Author{      public readonly string Name { get; }    public readonly int Article { get; }    public readonly string Branch { get; }    public Author(string name, int article, string branch)    {          this.Name = name;        this.Article = article;        this.Branch = branch;    }}  class GFG {      // Main method    static public void Main()    {        Author a = new Author("Rohit", 67, "CSE");        Console.WriteLine("Author name: " + a.Name);        Console.WriteLine("Total articles: " + a.Article);        Console.WriteLine("Branch name: " + a.Branch);    }}
Output:
Author name: Rohit
Total articles: 67
Branch name: CSE

3. Readonly Members: This feature is introduced in C# 8.0. In this feature, you are allowed to use a readonly modifier to any member of the structure. This readonly modifier specifies that the member is not allowed to modify. It is better than applying the whole structure readonly.
Example:
// C# program to illustrate how // to create a readonly memberusing System;  public struct Customer{      public string Name { get; }    public int Price { get; }      // Readonly member    public readonly string Product { get; }      public Customer(string name, string product, int price)    {          this.Name = name;        this.Product = product;        this.Price = price;    }}  class GFG {      // Main method    static public void Main()    {        Customer a = new Customer("Sumit", "Mobile Phone", 2398);        Console.WriteLine("Customer name: " + a.Name);        Console.WriteLine("Product: " + a.Product);        Console.WriteLine("Price: " + a.Price);    }}
Output:
Customer name: Sumit
Product: Mobile Phone
Price: 2398

CSharp-keyword
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.