The question is a bit confusing. Let us define some members:
A constant expression (roughly) is an expression known to the compiler as a concrete constant value.
A literal is a special kind of constant expression; 123 and Math.PI are constant expressions. The first is literal, the last is not.
A constant field is a member of a type that is initialized with a constant expression and can then be used as a constant expression elsewhere. Math.PI is an example of a constant field.
A local constant as a constant field, but attached to a block. (Just as a local variable is bound to a block.)
Constant fields and local constants must explicitly indicate their type; there is no "var" form for constants. (The idea itself makes you tremble, "const var" is obviously an oxymoron.)
Local variables are not required to indicate their type; the type can be inferred from the initializer. Such a local variable is called an "implicitly typed local variable".
So your question is: "Is there a way to write a literal constant expression of type short that can be used to initialize an implicitly typed local variable of type short?"
No no. You can explicitly enter a local variable:
short s1 = 123;
You can explicitly enter a local constant:
const short s2 = 123;
Or you can make a constant expression that contains a short press:
var s3 = (short)123;
Or you can create a local or field constant and use its name to initialize an implicitly typed local:
var s4 = s2;
But there is no way around; short should appear somewhere in the field or local declaration or listing.
Eric Lippert Dec 29 '11 at 17:10 2011-12-29 17:10
source share