85

Is there a way to specify that my variable is a short int? I am looking for something similar to M suffix for decimals. For decimals, I do not have to say

var d = (decimal)1.23;

I can just write as follows:

var d = 1.23M;

Is there a way to write this

var s = SomeLiteralWithoutCast

so that s is implied to be short int?

Arne Lund
  • 2,366
  • 3
  • 26
  • 39

6 Answers6

149

Short answer, No. In C#, there's no letter S that could be used as var a = 123S that would indicate that a is of type short. There's L for long, F for float, D for double, M for decimal, but not S. It would be nice if there was, but there isn't.

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;
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
25

The question is a bit confusing. Let's define some terms:

A constant expression is (roughly speaking) an expression known to the compiler to be a particular constant value.

A literal is a particular kind of constant expression; 123 and Math.PI are both constant expressions. The former is a literal, the latter is not.

A constant field is a member of a type that is initialized with a constant expression, and may then be used as a constant expression elsewhere. Math.PI is an example of a constant field.

A local constant is like a constant field, but scoped to a block. (Just as a local variable is scoped to a block.)

Constant fields and local constants are required to state their type explicitly; there is no "var" form for constants. (The very idea makes one shudder; a "const var" is obviously an oxymoron.)

Local variables are not required to state 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, there is not. You can explicitly type the local variable:

short s1 = 123;

You can explicitly type a local constant:

const short s2 = 123;

Or you can make a constant expression that contains a cast to short:

var s3 = (short)123;

Or you can make a local or field constant and use its name for the initializer of the implicitly typed local:

var s4 = s2;

But there is no way around it; short has to appear somewhere, either in a field or local declaration or in the cast.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 6
    Why there isn't a suffix for short literal? – gdoron Dec 29 '11 at 18:26
  • 1
    @gdoron: All features are unimplemented by default until someone implements them. There's no suffix for short literal because no one ever designed, implemented, tested and shipped that feature. More specifically: **why should there be**? What's the compelling benefit of having a suffix for a short literal **in C# 1.0**? The absence of a feature does not need to be justified; if you think a feature is a good idea then the person who *wants* the feature has to justify it. What's the justification? – Eric Lippert Dec 29 '11 at 19:00
  • 5
    @gdoron: There are justifications other than var. For example, you have two methods M(int) and M(short) and you want to force a call to the short overload: `M((short)123)` but without the cast. The benefit of eliminating the cast in that scenario is small compared to the cost. (And in that case the cast is arguably better, as it makes it crystal clear which overload is being called.) – Eric Lippert Dec 29 '11 at 19:34
  • 17
    @EricLippert I've often wondered why the C# team implemented suffixes for some numeric primitive types but not others. – James Dec 31 '11 at 12:21
  • 1
    Yes, what James said. Presumably there was justification for long/float/double being implemented; why did this not apply to short? – Jon Jan 02 '14 at 06:15
  • 4
    Having short literals would also make code like `short x = y < 0 ? (short)0 : (short)y` cleaner (`short x = y < 0 ? 0S : (short)y`) – Clément Jan 16 '14 at 14:48
  • 1
    Eric, The justification is the same as for the other suffixes, so you can represent the value in an arithmetic expression (like `myShortVariable + 23s` without having to cast it (like `myShortVariable + (short)23`. and the comment that `there is no way around, short has to appear somewhere ...` is kinda not on point... It doesn't apply to `decimal`, if you write the expression `var mydecimalVariable = myotherdecimal + 23.00m;` the word decimal doesn't appear anywhere. – Charles Bretana Sep 11 '15 at 18:25
  • I know it's old but and what I'm about to say is probably already known by everyone... But more justification for having a 'short' suffix incoming. `short i = 10` is the same as `short i = (short)10` and `var i = (short)10`. Since c#'s default for literals is Int32, you're always casting when you try to assign a short even if the type of the current object is short. It's just an implicit conversion operator at work. – RoyalPotato Aug 14 '16 at 20:11
  • Old topic/answer, but just wanted to discuss this statement: _A literal is a particular kind of constant expression;_. `10m` is a literal decimal but is not a constant, and is instead translated to `new decimal(10)` or something similar, right? – julealgon Jun 07 '19 at 14:12
  • @julealgon a decimal literal is a constant. How it is encoded depends on where the expression is, but yes it can be encoded as a constructor invocation. – Eric Lippert Jun 07 '19 at 14:45
  • @julealgon: Try analyzing `using System; public class C { const Decimal x = 1.0m; public void M() { const Decimal y = 2.0m; } }` on sharplab.io and see what you get. Do you see why constant decimals are encoded differently depending on context? Can you think of other situations where decimal constants might not be encoded as ctor calls? – Eric Lippert Jun 07 '19 at 15:04
10

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

short s = 123;
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
8

Two options; neither ideal:

  1. Remove the var, specifying the type explicitly:

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

    var s = (short)123;
    

That's the only options for specifying a literal short.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

You can use the following:

var value = (short)123;

Of course it doesn't really make sense since the whole point of var is not to write the type.

Toni Parviainen
  • 2,217
  • 1
  • 16
  • 15
0

There is no such thing Implicitly const So you will have to define your const as short like this:

const short x = 999;

see more here

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367