Sunday 6 November 2016

Constant Vs ReadOnly

Constants


Constants are declared using "const" keyword.

By default const is static and we cannot change the value throughout the entire program.

Constants should be assigned a value at the time of declaring a variable.

Constant variables have to be accessed using ClassName.VariableName

Constants are known at compile time.

Look at Examples Below: 




If you observe the above example i have not initialized the co variable, it gives me an error like " A Const field requires a value to be provided". 

Let's try to change the value. 






Read Only


Read Only is the keyword whose value we can change during run time.

Read Only can change at Run time but only through the Non Static Constructor, Not even a method

Read Only variables have to be accessed using "InstanceName.VariableName"

Read Only Variables are known at run time.





Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      



    




No comments: