Ayyaz Ahmad
How constants and read-only variables are different?
By Ayyaz Ahmad in .NET on Jan 23 2016
  • Thiruppathi R
    May, 2016 27

    Constants =Compile Time. Read-Only=Run Time.

    • 2
  • Sandeep Singh
    Aug, 2016 18

    Constant variable is a fixed value for the whole class where as Read-Only variable is a fixed value specific to a particular Instance of the Class.

    • 1
  • Nayeem Mansoori
    Mar, 2016 22

    const: Can't be changed anywhere.readonly: This value can only be changed in the constructor. Can't be changed in normal functions.Example : Below my example which is show diffence between Const and ReadonlyWe have a Test Class in which we have two variables one is readonly and another is constant.class Test { readonly int read = 10; const int cons = 10; public Test() { read = 100; cons = 100; } public void Check() { Console.WriteLine("Read only : {0}", read); Console.WriteLine("const : {0}", cons); } } Here I was trying to change the value of both the variables in constructor but when I am trying to change the constant it gives an error to change their value in that block which have to call at run time. So finally remove that line of code from class and call this Check() function like the following code class Program { static void Main(string[] args) { Test obj = new Test(); obj.Check(); Console.ReadLine(); } } class Test { readonly int read = 10; const int cons = 10; public Test() { read = 100; } public void Check() { Console.WriteLine("Read only : {0}", read); Console.WriteLine("const : {0}", cons); } }

    • 1
  • Komara Reddy
    Oct, 2016 13

    we need to assign a value to constant variables at run time and it is constant through out the application,in case read-only we can assign a value at run time after that it is constant through out the application

    • 0
  • Nilesh Patel
    Sep, 2016 19

    In Constants variable is fixed value & Read only you can change value of at Run Time.

    • 0
  • Vishal Jadav
    Aug, 2016 6

    Constants are assigned design time and read-only assigned run time.

    • 0
  • Mangesh Kulkarni
    Jul, 2016 31

    We can not change constant value. We can change read-only value in constructor

    • 0
  • Pooja Singh
    Jul, 2016 6

    Constant =compiler Time, Read-only =run time. constant value is always constant but read -only value some time varies. example- constant= meterToCm , it is always constant. example- read-only=pie=3.1 but any other user written pie value is 3.14 then its varies value.

    • 0
  • sakthi smith
    Jun, 2016 27

    constant = cant change the value of the variable at the runtime read-only = can change the value of the variable at the runtime

    • 0
  • Keerthi Venkatesan
    Jun, 2016 8

    Cnstant and ReadOnly keyword are used to make a field constant which value cannot be modified. Static keyword is used to make members static that can be shared by all the class objects.

    • 0
  • kapil kumar
    Jun, 2016 7

    you cant change the value of const after declaration but Read only variables can be assigned values either at runtime or at the time of instance initialization via constructor

    • 0
  • Bhuvanesh Mohankumar
    May, 2016 29

    Const must be initialized at declaration time, readonly can be initialized on the constructor

    • 0
  • Prasanna Murali
    May, 2016 20

    constants doen't change the values during the compile time so it is called as a read-only variable

    • 0
  • Vivek Kumar
    Apr, 2016 17

    Constant variables must be assigned at compile time. Read-only variables can be assigned at compile time or run time.

    • 0
  • Sreekanth Reddy
    Mar, 2016 15

    Constant variables should be assigned at compile time. Read-only variables can be assigned at runtime.

    • 0
  • Pankaj  Kumar Choudhary
    Feb, 2016 9

    Constants are called compile time constant and read-only are called run time constant, Read only can be assigned in Constructor ...but u can't do the same with constants

    • 0
  • Nishant Mittal
    Feb, 2016 9

    Read only can be assigned in Constructor ...but u can't do the same with constants....

    • 0
  • Akhil Garg
    Jan, 2016 31

    Constant is compile time variables and Readonly is runtime variable

    • 0
  • Rajeev Punhani
    Jan, 2016 27

    Constants are the variables that are assigned value at compile time and they cannot be changed later.Read only variables can be initialised at the time of declaration or from constructor.

    • 0
  • Sujeet Suman
    Jan, 2016 27

    Constant is compile time variables whereas Readonly is runtime variable.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS