简体   繁体   English

.Net数字类型初始化标识符列表

[英]List of .Net Numeric Type Initialization Identifiers

I'm looking for a list numeric type initalization identifiers for both C# and VB.Net. 我正在寻找C#和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 VB.Net

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

C# C#

C# C#

Section 1.8 of the C# specification includes these values. C#规范的第1.8节包含这些值。

integer-type-suffix: one of U u L l UL Ul uL ul LU Lu lU lu 整数型后缀:U u L l UL Ul uL ul LU Lu lU lu之一

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

Section 2.4.4.2 elaborates on this for Integer types: 第2.4.4.2节详细阐述了整数类型:

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. 如果文字没有后缀,则它具有这些类型中的第一个,其值可以表示为: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. 如果文字后缀为U或u,则它具有这些类型中的第一个,其值可以表示为: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. 如果文字后缀为L或l,则它具有这些类型中的第一个,其值可以表示为:long,ulong。
  • If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong. 如果文字后缀为UL,U1,uL,ul,LU,Lu,lU或lu,则为ulong类型。

Section 2.4.4.3 elaborates on this for Real types: 第2.4.4.3节对Real类型进行了详细说明:

If no real type suffix is specified, the type of the real literal is double. 如果未指定实际类型后缀,则实数的类型为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. 以F或f为后缀的实数是float类型。 For example, the literals 1f, 1.5f, 1e10f, and 123.456F are all of type float. 例如,文字1f,1.5f,1e10f和123.456F都是float类型。
  • A real literal suffixed by D or d is of type double. 以D或d为后缀的实数是double类型。 For example, the literals 1d, 1.5d, 1e10d, and 123.456D are all of type double. 例如,文字1d,1.5d,1e10d和123.456D都是double类型。
  • A real literal suffixed by M or m is of type decimal. 以M或m为后缀的实数是十进制类型。 For example, the literals 1m, 1.5m, 1e10m, and 123.456M are all of type decimal. 例如,文字1m,1.5m,1e10m和123.456M都是十进制类型。 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). 通过获取精确值将此文字转换为十进制值,并在必要时使用银行家的舍入舍入到最接近的可表示值(第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). 除非值被舍入或值为零(在后一种情况下,符号和比例将为0),否则将保留文字中明显的任何比例。 Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3. 因此,将解析文字2.900m以形成带符号0,系数2900和比例3的小数。

VB VB

Similarly, the VB specification contains details for both Integer and Floating point literals. 类似地,VB规范包含IntegerFloating point文字的详细信息。

For integers: 对于整数:

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

For floating points: 对于浮点数:

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

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: C#编译器的一个有趣特性是它实际上将以下内容编译为同一个东西:

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 - ie 唯一一次它做任何不同的AFAIK是带有尾随零的十进制 - 即

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);
}

In VB you should have 在VB中你应该有

Option Strict On 选项严格打开

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM