Raba - Defend your code RSS 2.0
# Saturday, January 21, 2006

There are many people who asking me again and again about this question,
I've finally found some time to write about this and show you simple example for all of you out there so you can't forget this.

As we know (read somewhere but nor really remember) one is runtime and the other compile time...
so here is the code:
public const int IntConstField = 1;
public const string StringConstField = "shani";
public static readonly int IntROField = 1;
public static readonly string StringROField = "shani";

and this the usage:
Console.WriteLine(Class1.IntROField);
Console.WriteLine(Class1.StringROField);
Console.WriteLine(Class1.IntConstField);
Console.WriteLine(Class1.StringConstField);

Isn't It look the same?
so I disassemble this one for you...

IL_000a: ldsfld int32 [TestLib]TestLib.Class1::IntROField
IL_000f: call void [mscorlib]System.Console::WriteLine(int32)
IL_0014: ldsfld string [TestLib]TestLib.Class1::StringROField
IL_0019: call void [mscorlib]System.Console::WriteLine(string)

IL_000a: ldc.i4.1
IL_000b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0010: ldstr "shani"
IL_0015: call void [mscorlib]System.Console::WriteLine(string)

The first four rows show the Readonly, where you can see the ldsfld which is MSIL instruction for pushing the value of a static field into the stack!
The other four rows show the Const, Where you can see ldc,ldstr instead of ldsfld which means that Const doesn't behave like Readonly, Const is very similar to to #define in c, don't have its own location as global, Const is just written as number/string (hard coded) at the compile time.

After we know this simple transformation at the compile time,
we will see an example including two DLLs:

A: hold the const vars.
B: uses A's const vars.
When we made a change at A (changing one of the consts) we can't only compile it and replace the DLL at the bin directory, we should also compile this with the DLL who use it (B in our case).
So it will change also the hard coded numbers!

PI, is a good const, but Unit Number or Dept Number (=180) isn't good enough.

Monday, January 23, 2006 8:18:48 PM (GMT Standard Time, UTC+00:00)
Hey Shani,
Great work and great examples, keep it up!
Tuesday, January 24, 2006 8:47:48 PM (GMT Standard Time, UTC+00:00)
Thanks man,
I am glad to see that you keep reading my blog.
Friday, March 10, 2006 4:25:37 PM (GMT Standard Time, UTC+00:00)
Very interesting article and very helpful blog! Thanks!
Wednesday, January 17, 2007 7:09:39 AM (GMT Standard Time, UTC+00:00)
What is the difference between Const and Readonly?
praveen
Comments are closed.
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Shani Raba
Sign In
Statistics
Total Posts: 145
This Year: 0
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2012, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)