How to specify short int constant without casting?

Is there a way to indicate that my variable is a short int? I am looking for something similar to the M suffix for decimal places. For decimal places I don't need to say

var d = (decimal)1.23; 

I can simply write the following:

 var d = 1.23M; 

Is there any way to write this

  var s = SomeLiteralWithoutCast 

so s is meant as short int?

+57
Dec 29 '11 at 16:34
source share
6 answers

Short answer no. In C # there is no letter S , which can be used as var a = 123S , which indicates that a is of type short . There L for long , F for float , D for double , M for decimal , but not S It would be nice if it were, but no.

 var a = 1M; // decimal var a = 1L; // long var a = 1F; // float var a = 1D; // double var a = 1; // int var a = 1U; // uint var a = 1UL; // ulong 

but not

 var a = 1S; // not possible, you must use (short)1; 
+94
Dec 29 '11 at 16:53
source share
β€” -

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.

+23
Dec 29 '11 at 17:10
source share

There is no suffix for the short data type in C #. If you want the integer literal to be short, you need to explicitly specify the type and provide the literal in range.

 short s = 123; 
+9
Dec 29 '11 at 16:35
source share

Two options; nor ideal:

  • Remove var by explicitly specifying the type:

     short s = 123; 
  • Use the cast syntax (noting that this is a compile-time operation, not a run-time):

     var s = (short)123; 

These are the only options to indicate a literal short circuit.

+8
Dec 29 2018-11-12T00:
source share

You can use the following:

 var value = (short)123; 

Of course, this does not make sense, since the whole var point should not write a type.

+2
Dec 29 '11 at 16:38
source share

There is no such thing Implicitly const Therefore, you will need to define your const as short:

 const short x = 999; 

more details here

0
Dec 29 '11 at 16:39
source share



All Articles