Wednesday, October 05, 2011

Internals of Constants and Readonly


In this article i shall be discussing about constants and read only fields which we all use in our .NET variant language codes viz C#, VB .NET, VC++, etc.


Constants are not changeable values in the entire code base once it is defined.

Lets dig a bit deeper into the const keyword which we use in our C# code. Basically, constants are by default/implicitly static for the scope in which it is defined. Also note that there wont be any memory created for the constants by the run time. So what happens under the hood, lets see via IL and a C# code.

public static void Main(string[] args)
            const int TCPPortNumber = 25;

As you can see from the above image, i have used a constant inside the main method. Now lets look at the IL (using ILSPY/ILDASM) to see if there is any memory created for the variable or not:


Read more: Codeproject
QR: Constants_Readonly.aspx

Posted via email from Jasper-Net