Readonly and constant members

What is the difference between Readonly and constant members?


Both Readonly and const data members have constant value and can't be changed. But the real this is explained below along with the code.


Readonly Data Members


Readonly are instance data members.


A readonly data member can't be made property and can be changed in class constructor only.


Const Data Members


Const data members are static and only can be used with class reference and the const data member name.


using System;


namespace readonly_const


    class Program



        public class test



            public readonly string name = "George";

            public const string coname = "ABC Company LLC";


            public test(string name)


                // you can change the readonly data members only in class constructor

       = name;




//We can't write a set property for readonly data members

            public string _name




                    return name;





                //    name = value;





        static void Main(string[] args)



            test obj = new test("Tina Turner");




            // You can only change the value of readonly data memebers in class constructor


            // = "Rocky";







Similar Articles