Dobkin's Place

Wednesday, November 08, 2006

Const vs. Static Rreadonly

If you declare a variable, within the same scope, using those declaration statements you got the same result – a variable that is initialized in declaration and can't be modified after. So what really the difference?
The main difference is in the way those two declarations are compiled. Const statement saved in to the project output in compile time as is, and static readonly statement is set only when it's evaluates first time at run time. In fact what is saved within the project output is a link to this field and not the data itself.
The way this little difference affects us is: when we use a reference to pre compiled assembly. We change the value of the const filed and recompile the assembly without recompiling our core project (which uses this reference). The change, which we have just made, won't take place because the const value has been already saved within project output. As opposite to static readonly field, which modification will take palace in referencing project, without recompiling it.

Bottom line, declaring variables as static readoonly is more efficient and safe way to declare non modified variables for your module.

0 Comments:

Post a Comment

<< Home