In .NET, if I define a constant (say a private constant in a class), will referencing that constant cause the value to be repeated in memory wherever it is referenced, or does .NET just use a pointer to where the constant is stored, the same as using any normal variable?
Share
It is incorporated in the IL (i.e. “repeated”). The alternative is to use the readonly keyword.
Do note that this is what makes a public const very dangerous. A bug fix release may alter the value but any other assemblies that use the const still use the old value. Only ever declare constants internal or private. Use readonly for public declarations. Unless it is a “manifest constant” that requires a change in the fabric of the universe. Like Math.Pi