Friday, July 01, 2005

difference between readonly and const

The big difference between constants and readonly variables is that a readonly variable can receive its value at run time and a constant receives its value at compile time.

Effectively, it produces the same result—you end up with a variable that cannot change.

The following example declares a constant to hold the date March 12, 2002. That date can never change—it is compiled into the program. The second declaration is for a readonly variable that is initialized at run time to the current date.
const string TheDate = "03/09/2002";
public static readonly string TheDateRO = new DateTime().ToString();

0 Comments:

Post a Comment

<< Home