Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of .Net Numeric Type Initialization Identifiers

Tags:

c#

.net

vb.net

I'm looking for a list numeric type initalization identifiers for both C# and VB.Net.

for example:

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

Here's the list:

The identifiers are case-insensitive

VB.Net

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

C#

like image 752
Micah Avatar asked Mar 26 '09 14:03

Micah


2 Answers

C#

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 elaborates on this for Integer types:

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.

Section 2.4.4.3 elaborates on this for Real types:

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.

VB

Similarly, the VB specification contains details for both Integer and Floating point literals.

For integers:

ShortCharacter ::= S
IntegerCharacter ::= I
LongCharacter ::= L

For floating points:

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

like image 198
Jeff Yates Avatar answered Oct 13 '22 12:10

Jeff Yates


Personally, I don't always use the identifiers, for exactly the reasons (memory) raised here. An interesting feature of the C# compiler is that it actually compiles the following to 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 find the second version far more readable. So I use that approach. The only time AFAIK that it does anything different is with decimal 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);
}
like image 28
Marc Gravell Avatar answered Oct 13 '22 12:10

Marc Gravell