const means static (you don't need an instance to refer to the const value).
I also want to add this important point: when you link to a (link) assembly with public const , this value is copied to your assembly. Therefore, if the const value in the specified assembly changes, your assembly will still have the original compiled value.
If this behavior is unacceptable, you should consider creating a public static readonly field.
Lib.dll presented as binary:
public class Foo { public const int HATS = 42; public static readonly int GLOVES = 33; }
App.exe, lib.dll links:
Foo.HATS // This will always be 42 even if the value in Lib.dll changes, // unless App.exe is recompiled. Foo.GLOVES // This will always be the same as Foo.GLOVES in Lib.dll
From MSDN :
Do not create a constant to represent the information that you expect to change at any time. For example, do not use a permanent field to store the price of a service, product version number, or company name. These values โโmay change over time, and since compilers distribute constants, other code compiled with your libraries must be recompiled to see the changes.
From DotNetPerls :
DLLs. When you use a field or a const declaration, the C # compiler actually injects the value of the const variable directly into the IL code. Therefore, it essentially erases const as a separate object.
Caution: If programs that depend on const are not recompiled after changing the value of const , they may break [because they will continue to use the previous value].
Jonathon Reinhart Oct 31 '12 at 2:23 2012-10-31 02:23
source share