Read Only Property in C# Language

Introduction

Read only member in a c sharp programming, It is represent a unchanging value, which like a constant value. But read only member can be initialized at run time, not a compile time . Its mean read only member are change only compile time, which is possible by constructor, and after compile you have no change the value of read only member. For example:   

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace dynamic_object_in_c_sharp

{

    public class key  //create class

    {

        readonly int x = 10; //define read only member value

 

        public key()        //create constructor

        {

            Console.WriteLine("Predefine Readonly Member Value     : {0}",x); //Before compile read only value

            x = 12; //change value of read only member in compile time           

        }

        public void show()

        {

            Console.WriteLine("\nAfter Compile Readonly member value :{0}{1}", x, "  Change");//after compile read only value

            Console.ReadLine();

        }                      

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            key k = new key(); //create object of class key

            k.show();  //call to show function

        }     

    }

}


Output:

output.jpg


Next Recommended Reading History Of C# Programming Language