Wednesday, February 11, 2009

Advantage of ReadOnly over Constant variables in dotnet

Constant variables are compile time constants whereas ReadOnly variables are runtime constants. Compile-time constants are slightly faster, but far less flexible, than runtime constants. Reserve the compile-time constants for when performance is critical and the value of the constant will never change over time.

EXAMPLE:
// Compile time constant:public const int _Millennium = 2000;
// Runtime constant:public static readonly int _ThisYear = 2004;

Also, you can use readonly values for instance constants, storing different values for each instance of a class type. Compile-time constants are, by definition, static constants.

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory