29

this:

const int a = 5;

compiles just fine, whereas

const var a = 5;

doesn't... while:

var a = 5;

compiles just as well as this:

int a = 5;

why?

bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • Let me see if I get the this straight... You want to know "Why you can't **dynamically** type and **constant** value?"... maybe because it if don't know what type it is, the last thing you want do is make it constant? – Cos Callis May 26 '11 at 00:28
  • How about this? `const` is shorthand for "constant"; `var` is shorthand for "variable". Constants and variables are polar opposites, making `const var` an oxymoron. – LukeH May 26 '11 at 00:33
  • 22
    @Cos var doesn't dynamically type. It just tells the compiler "hey, figure this variables type out yourself." – bevacqua May 26 '11 at 00:35
  • Then what of your definition of "dynamic" that doesn't include "figure it out yourself"? – Cos Callis May 26 '11 at 00:51
  • 22
    @Cos Callis: variables declared with `var` are **statically** typed using type-inference (http://en.wikipedia.org/wiki/Type_inference) at compile time. – Juliet May 26 '11 at 01:24
  • 1
    Hm, but if memory serves me right, in C++, where you have the "auto" keyword for essentially the same purpose, you can make it const. And why shouldn't you ?constness has nothing to do with the type it represents. It's just there to tell us, that the intent for the usage of this bit of memory is to remain unchanged. – Mr.Infredible Mar 11 '21 at 07:45

4 Answers4

21

The var keyword was intended to save you from writing long complex typenames, which cannot be constants.

It is very convenient to be able to write declarations like

var dict = new Dictionary<string, List<Definition>>();

It becomes necessary when using anonymous types.

For constants, this isn't an issue.
The longest built-in typename with constant literals is decimal; that's not a very long name.

It is possible to have arbitrarily long enum names which can be used as constants, but the C# compiler team apparently wasn't concerned for that.
For one thing, if you're making a constant enum value, you might as well put it in the enum.
Also, enum names shouldn't be too long. (Unlike complex generic types, which can and frequently should)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 5
    So, in summary, 'just because'? – Kirk Broadhurst May 26 '11 at 00:25
  • 1
    @Michael - I see this answer explains why you might not really *need* to use `var` for constants, but it doesn't explain why you *can't*. – Kirk Broadhurst May 26 '11 at 01:10
  • 2
    @Kirk - One implies the other, or at least should - if something's not really useful, it shouldn't be done. Eric Gunnerson explains it with [different words](http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx): *every feature starts off with negative 100 points*. – Michael Petrotta May 26 '11 at 01:29
  • @Michael - Good point, I like that explanation. – Kirk Broadhurst May 26 '11 at 01:42
  • 17
    Yes `const var` is dumb, but if `var` is a convenience, then `const` should be able to do the same thing: `const s = "mystring"`. http://stackoverflow.com/a/2614617/1037948 – drzaus Jun 21 '13 at 15:00
  • 3
    var isn't just about brevity and convenience. It's also about redundancy. Writing the type twice is against the DRY (don't repeat yourself) principle. – Lensflare Nov 28 '14 at 18:59
  • 4
    It is wrong to say that type inference on constants is pointless or useless. It may be a limitation by C#, but it would be useful. Apple's programming language Swift has type inference for constants and it makes perfect sense there. – Lensflare Nov 28 '14 at 19:06
  • In C++11 you can say, e.g. `const auto i = 5`. It's not pointless or useless with more complex types. There's no reason why it couldn't work also in C# with `var`. – juzzlin Mar 13 '18 at 13:45
9

It is a compiler limitation, and the reason for that limitation is given by Eric Lippert here

Martin Booth
  • 8,485
  • 31
  • 31
  • Eric lippert has left a comment to this question which might be more definitive: http://stackoverflow.com/questions/2128432/type-inferring-a-constant-in-c – Martin Booth May 26 '11 at 00:42
2

Constants without var:

const int Value1 = 1;
const int Value2 = 2;

Constants with var (anonymous type property values cannot be changed after creation):

var constants = new { 
  Value1 = 1, 
  Value2 = 2,
};
//use as constants.Value1
vvv
  • 21
  • 1
0

Since constants must be built-in numeric types or string, you don't really save much; const int is the same length as const var and int is probably the most common type of constant. Then there's double which is really not all that long. If you have a lot of them to type, use the Alt selection feature ;-)

Ry-
  • 218,210
  • 55
  • 464
  • 476