20
Reply

How constants and read-only variables are different?

Ayyaz Ahmad

Ayyaz Ahmad

Jan 23, 2016
3.3k
0

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

    Thiruppathi R
    May 27, 2016
    2

    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.

    Sandeep Singh
    August 18, 2016
    1

    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); } }

    Nayeem Mansoori
    March 22, 2016
    1

    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

    Komara Reddy
    October 13, 2016
    0

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

    Nilesh Patel
    September 19, 2016
    0

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

    Vishal Jadav
    August 06, 2016
    0

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

    Mangesh Kulkarni
    July 31, 2016
    0

    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.

    Pooja Singh
    July 06, 2016
    0

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

    sakthi smith
    June 27, 2016
    0

    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.

    Keerthi Venkatesan
    June 08, 2016
    0

    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

    kapil kumar
    June 07, 2016
    0

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

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

    Prasanna Murali
    May 20, 2016
    0

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

    Vivek Kumar
    April 17, 2016
    0

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

    Srikanth Reddy
    March 15, 2016
    0

    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

    Pankaj Kumar Choudhary
    February 09, 2016
    0

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

    Nishant Mittal
    February 09, 2016
    0

    Constant is compile time variables and Readonly is runtime variable

    Akhil Garg
    January 31, 2016
    0

    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.

    Rajeev Punhani
    January 27, 2016
    0

    Constant is compile time variables whereas Readonly is runtime variable.

    Sujeet Suman
    January 27, 2016
    0