in C #, we have a const or readonly keyword to declare a constant.
Const
The constant member is determined at compile time and cannot be changed at run time. Constants are declared as a field using the const keyword and must be initialized as they are declared. For instance:
public class MyClass { public const double PI = 3.14159; }
PI cannot be modified in the application anywhere else in the code, as this will cause a compiler error.
only for reading
A read-only member as a constant in that it represents an immutable value. The difference is that the readonly element can be initialized at run time, and the constructor can be initialized as they are declared. For instance:
public class MyClass { public readonly double PI = 3.14159; }
or
public class MyClass { public readonly double PI; public MyClass() { PI = 3.14159; } }
Because the readonly field can be initialized either in the declaration or in the constructor, readonly fields can have different values โโdepending on the constructor used. The readonly field can also be used for runtime constants, as in the following example:
public static readonly uint l1 = (uint)DateTime.Now.Ticks;
Notes
readonly members are not implicitly static, and therefore a static keyword can be applied to the readonly field explicitly if necessary.
The readonly element can contain a complex object using the new keyword during initialization. readonly members cannot contain enumerations.
the loan goes here: http://www.dotnetspider.com/forum/69474-what-final-ci-need-detailed-nfo.aspx
source share