29

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?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

5 Answers5

39

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).

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • 2
    Also a static readonly can be changed in a constructor, so different constructors can provide different values for the static readonly field. – Waleed Al-Balooshi Mar 25 '10 at 03:55
15

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.

itowlson
  • 73,686
  • 17
  • 161
  • 157
6

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).

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • This doesn't sound right. Static is valid on a field as well. And the only possible const reference type is a string. You can't change a string. – Hans Passant Mar 25 '10 at 03:52
  • Also worth noting that the only two reference types a const can hold is string and null – Waleed Al-Balooshi Mar 25 '10 at 03:53
  • It sounded wrong to me, too, right after I wrote it. So I went and checked -- you _can_ have a const of reference types. I _knew_ that was possible. But except for strings when you do it must be null. Updating my answer to reflect that. – Joel Coehoorn Mar 25 '10 at 03:55
  • Reference is here: http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.85).aspx `A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and null.` – Joel Coehoorn Mar 25 '10 at 03:58
3

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.

Thomas
  • 63,911
  • 12
  • 95
  • 141
0

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.

anwar
  • 1