开发者

What is the difference between const and static in C#?

I am eager to know the difference between a const variable and a static variable.

Is a const variable also always 开发者_运维知识库static? What is the difference between them?


const fields can only hold value types or System.String. They must be immutable and resolvable at compile-time.

static readonly fields can and generally do hold reference types, which (other than strings) can only be created at runtime. These can (but shouldn't) be mutable types; the only thing that cannot change is the reference itself.

If you need to maintain a "constant" set of instances that are reference types, you generally do it with a set of public static readonly fields, such as the members of System.Drawing.SystemColors.

Last but not least, initialization of a readonly field can be deferred until the execution of a constructor, which means that it even though it can only be written to once, it does not always have to be initialized with the exact same value. True constants declared with const can only ever have a single value (specified at compile time).


One subtle but crucial difference is that consts are evaluated at compile time, whereas statics are evaluated at run time. This has an important impact on versioning. For example, suppose you write:

public const int MaxValue = 100;

You compile and ship your assembly (Assembly A). Then someone else writes an assembly (Assembly B) which references MaxValue. In this case, the value 100 is compiled into their assembly as well as yours.

If you had written this:

public static readonly int MaxValue = 100;

then the reference in their assembly would remain just that, a reference. When someone ran Assembly B, the value 100 would be loaded from your assembly, Assembly A.

This can, for example, affect simple patching scenarios. If you issue an updated Assembly A where MaxValues is declared as 200, and the user copies that version over the previous version (but does not recompile Assembly B), then in the first scenario Assembly B will continue to operate as if MaxValues were 100, because that's the const value that was compiled into Assembly B. In the second scenario, Assembly B will pick up the new value because it loads the non-const static variable at runtime.


As you say, both static and const are attached to a type rather than an instance of a type. However, you can still change static items. You cannot change const items.

Be careful with this, though. If your const item is a reference type, the assigned expression has to be evaluated at compile time, and that means that the only possible value you can give the reference is null (with the notable and useful exception of strings).


A (non-readonly) static can be changed after it has been declared whereas a constant cannot. In addition, a constant cannot be set using a function whereas a static variable can.


A constant is a variable that cannot be changed in value.

A static is a variable that cannot be used outside the scope of it's declaration. That is, if it is a global variable then it can only by used in the file that declares it. If it is a variable inside a function, then it can be use only inside that function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜