C# short/long/int literal format?
Asked Answered
S

5

281

In C/C#/etc. you can tell the compiler that a literal number is not what it appears to be (ie., float instead of double, unsigned long instead of int):

var d = 1.0;  // double
var f = 1.0f; // float
var u = 1UL;  // unsigned long

etc.

Could someone point me to a list of these? I'm specifically looking for a suffix for short or Int16.

Sanctus answered 28/4, 2011 at 15:3 Comment(2)
see here: https://mcmap.net/q/89048/-c-compiler-number-literalsClaudclauddetta
possible duplicate of Defining different types of numbers in C#Refrigerate
H
451
var d  = 1.0d;  // double
var d0 = 1.0;   // double
var d1 = 1e+3;  // double
var d2 = 1e-3;  // double
var f  = 1.0f;  // float
var m  = 1.0m;  // decimal
var i  = 1;     // int
var ui = 1U;    // uint
var ul = 1UL;   // ulong
var l  = 1L;    // long

I think that's all... there are no literal specifiers for short/ushort/byte/sbyte

Henderson answered 28/4, 2011 at 15:7 Comment(7)
Does this mean you have to cast everywhere you use short/ushort/byte/sbyte? Eg.: somebyte = somebool ? (byte) 1 : (byte) 0;Defeatist
@mola, yes, unless the desired type is unambiguous (e.g. byte b = 42;)Henderson
@Defeatist somebyte = (byte)(somebool ? 1 : 0);Haphazard
Just to add that upper case and lower case of these literal suffixes are equivalent e.g. 1l and 1L both will be treated as long integer but certainly 1L is more readable than 1l.Philately
I think you missed 1e+10 / 1e-10Topee
these lack github.com/dotnet/csharplang/issues/1058Gaiter
@DzmitryLahoda what do you mean? The suffixes discussed in that issue are not implemented yet.Henderson
P
54

From Integer literals:

The type of an integer literal is determined as follows:

  • If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.
  • If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong.
  • If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong.
  • If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.

And from Real literals:

If no real type suffix is specified, the type of the real literal is double. Otherwise, the real type suffix determines the type of the real literal, as follows:

  • A real literal suffixed by F or f is of type float. For example, the literals 1f, 1.5f, 1e10f, and 123.456F are all of type float.
  • A real literal suffixed by D or d is of type double. For example, the literals 1d, 1.5d, 1e10d, and 123.456D are all of type double.
  • A real literal suffixed by M or m is of type decimal. For example, the literals 1m, 1.5m, 1e10m, and 123.456M are all of type decimal. This literal is converted to a decimal value by taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding (Section 4.1.7). Any scale apparent in the literal is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
Priming answered 28/4, 2011 at 15:7 Comment(0)
O
10

If your variable isn't already a short, you have to cast it explicitly :

Object s = (Int16) 1;
Oshinski answered 14/3, 2013 at 16:0 Comment(2)
Side note: I think this causes a boxing conversion.Sanctus
It does cause boxing because Int16 is a value type and Object is a reference type.Sycophant
A
6

There isn't one for short. Just use short s = 1;.

Auditory answered 28/4, 2011 at 15:7 Comment(2)
Interestingly this compiles: short z1 = (0 == 1 ? 0 : 1); But this does not: short y = 1; short z2 = (0 == 1 ? 0 : y); (sorry, no line breaks allowed in comments)Excoriate
@yoyo: That's because the 0 in the if branch is converted to an int before the assignment to z2. If you use two shorts in your branches, the result will again be a short. With the if/else, the compiler can't know that your int can be represented as short.Scotty
F
2
var myValue = unchecked((short)0x7F00);

The literal is int and thus must be cast to target type. If a value overflow occurs unchecked is required.

Folio answered 26/1, 2022 at 5:51 Comment(1)
This is the answer for the original questionLangevin

© 2022 - 2024 — McMap. All rights reserved.