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();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s