Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving constants to a separate assembly [closed]

I have a project which utilizes many global constants. Eventually I am planning to extract some parts of the program and create assemblies of its own.

Is it worthy (or is it possible?) to create an assembly for global constants alone (viz, GlobalConstants.dll), so that I can use this assembly in future assemblies?

This method will help me do less coding, and I can keep the same name for the constants across the projects.

like image 417
logeeks Avatar asked Mar 29 '11 10:03

logeeks


1 Answers

While it is certainly possible to do and maybe even a pretty good idea, you should be aware of the fact that the C# compiler treats constants as values and not as references.

With this I mean that the C# compiler will replace all instances of the constant in you code and replace the "variable" with the value.

This means that even if you update the assembly GlobalConstants.dll and copy it to one of the applications you have, you will need to recompile that application. Not doing so, will cause the application to use the old constant values.

To overcome this problem, you can simply use public static readonly instead of public const as the readonly modifier differs from the const in that it is treated by the C# compiler as a reference in code rather than a value.

like image 163
Mikael Östberg Avatar answered Oct 21 '22 08:10

Mikael Östberg