A list of Net type initialization identifiers.

I am looking for numeric type initialization identifiers for C # and VB.Net.

eg:

Dim x = 1D 'Decimal' Dim y = 1.0 'initializes to float' 

Here is the list:

Identifiers are not case sensitive

Vb.net

  Int32 = 1, I
 Double = 1.0, R, 1.0e5
 Decimal = D
 Single = F,!
 Int16 = S
 UInt64 = L, UL

WITH#

+4
source share
3 answers

WITH#

Section 1.8 of the C # specification includes these values.

integer-type-suffix: one of U u L l UL Ul uL ul LU Lu lU lu

real type suffix: one of F f D d M m

Section 2.4.4.2 details this for Integer types:

The type of an integer literal is defined as follows:

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

Section 2.4.4.3 details this for real types:

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

  • A valid literal whose suffix F or f is of type float. For example, the literals 1f, 1.5f, 1e10f, and 123.456F are all of type float.
  • A valid literal whose suffix D or d is of type double. For example, the literals 1d, 1.5d, 1e10d and 123.456D are all of type double.
  • A valid literal whose suffix M or m is of type decimal. For example, literals 1m, 1.5m, 1e10m and 123.456M are all decimal types. This literal is converted to decimal value by taking the exact value, and, if necessary, rounding to the nearest representable value using bank rounding (section 4.1.7). Any obvious scale in the literal is preserved if the value is not rounded, or the value is zero (in the latter case, the sign and scale will be 0). Consequently, a literal 2.900 m will be parsed to form a decimal fraction with the sign 0, coefficient 2900 and scale 3.

Vb

Similarly, the VB specification contains parts for both Integer and Floating Point .

For integers:

ShortCharacter :: = S
IntegerCharacter :: = i
LongCharacter :: = L

For floating points:

SingleCharacter :: = F
DoubleCharacter :: = R
DecimalCharacter :: = D

+7
source

Personally, I do not always use identifiers, since it is the reasons (memory) that are raised here. An interesting feature of the C # compiler is that it actually compiles the following one and the same thing:

 static void Foo() { var x = 100F; Console.WriteLine(x); } static void Bar() { var x = (float)100; // compiled as "ldc.r4 100" - **not** a cast Console.WriteLine(x); } 

I believe the second version is more readable. Therefore, I use this approach. The only time AFAIK does something else, with a decimal point with trailing zeros - i.e.

 static void Foo() { var x = 100.00M; Console.WriteLine(x); } static void Bar() { var x = (decimal)100.00; // compiled as 100M - extended .00 precision lost Console.WriteLine(x); } 
+3
source

In VB you must have

Strict On Option

 Dim x as Decimal = 1D 'Decimal' Dim y as Double = 1.0 'initializes to double' Dim z as Integer = 1 'integer 
0
source

All Articles