Thursday, February 14, 2008

C# keywords: readonly vs const

The readonly keyword is different from the const keyword. 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.

In a nut shell we can say that, 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;

Refrences: MSDN July 2004

namaste !

1 comment:

Deepak said...

I need to edit XML file using javascript and HTML Only is it possible

plz help