Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Posts Tagged ‘Readonly

Difference between ReadOnly and Constant

leave a comment »

To get you understand this there are two ways.. One is to take you on a ride on source code as well as IL of the generated code. :). But currently 2:00 AM in the night and i feel sleepy.

So, i am taking shortcut on this,

The simple difference in one line is “Constant is replaced on compile time and readonly is variable which is read only and referred everytime”

If you define constant and use it somewhere in the code, compile and make an exe, then at each places where constant is used, replaced by its value. And that is the reason that constant once define could not be changed.

While if you use readonly, it still use reference of that variable. So readonly variable can be changed in the constructor after it’s defination.

If you still don’t get it what i mean, you can look into http://community.bartdesmet.net/blogs/bart/archive/2006/04/04/3867.aspx

🙂

Advertisements

Written by Manish

March 25, 2009 at 8:22 pm

Posted in .NET, .NET 3.0

Tagged with ,