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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
varkeyword 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
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
enumnames 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
enumvalue, you might as well put it in theenum.Also,
enumnames shouldn’t be too long. (Unlike complex generic types, which can and frequently should)