Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




What is the difference between CONST and READONLY

Both are meant for constant values. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

				
readonly int b;
public X()
{
b=1;
}
public X(string s)
{
b=5;
}
public X(string s, int i)
{
b=i;
}

Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants, as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks; 

(this can not be possible with const)

Share this article   |    Print    |    Article read by 1738 times
Author:
Guest
Guest user is that user who have done some activity on website without login. Activity contains commenting on any article, uploading an image on website etc.
Related Articles: No related article
Related Interview Questions: