c#短/长/ int文字格式吗?

时间:2022-07-31 16:43:31

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:

在C / c# /等等中,你可以告诉编译器一个文字数字不是它看起来的样子。,float代替double, unsigned long而不是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.

有人能告诉我一份清单吗?我特别想找一个短或短的后缀。

5 个解决方案

#1


216  

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

我认为这是所有…对于短/ushort/byte/sbyte,没有文字说明符

#2


31  

From §2.4.4.2 Integer literals:

从§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 UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL LU LU LU LU LU LU LU LU LU LU LU LU LU或LU,或LU,或LU,或LU,则为ulong类型。

And from §2.4.4.3 Real literals:

和§2.4.4.3真正的文字:

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:

如果没有指定实类型后缀,则实文本的类型为double。否则,实类型后缀决定了实文本的类型,如下所示:

  • 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.
  • 以F或F为后缀的真正文字类型为float。例如,文字1f、1.5f、1e10f和123.456F都属于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.
  • 以D或D为后缀的真正文字类型为double。例如,文字1d、1.5d、1e10d和123.456D都是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.
  • 以M或M为后缀的真正文字是十进制的。例如,字面量为1m、1.5m、1e10m和123.456M都是小数。此文字通过使用确切的值,并在必要时使用银行家的舍入将其四舍五入到最接近的可表示值,从而转换为十进制值(第4.1.7节)。文字中任何明显的比例都保留了,除非该值是圆形的或值为零(在后一种情况下,符号和刻度为0)。因此,将解析字面上的2.9 m,并以符号0、系数2900和scale 3来构成小数。

#3


7  

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

如果你的变量还不是很短,你必须明确地表示:

Object s = (Int16) 1;

#4


3  

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

没有一个是短的。只用短的s = 1;

#5


-7  

If you want to avoid type casts that often causes warnings with resharper, short.Parse("1") may also be an alternative.

如果您想避免使用resharper、short.Parse(“1”)可能也是一种替代方法。

#1


216  

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

我认为这是所有…对于短/ushort/byte/sbyte,没有文字说明符

#2


31  

From §2.4.4.2 Integer literals:

从§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 UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL UL LU LU LU LU LU LU LU LU LU LU LU LU LU或LU,或LU,或LU,或LU,则为ulong类型。

And from §2.4.4.3 Real literals:

和§2.4.4.3真正的文字:

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:

如果没有指定实类型后缀,则实文本的类型为double。否则,实类型后缀决定了实文本的类型,如下所示:

  • 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.
  • 以F或F为后缀的真正文字类型为float。例如,文字1f、1.5f、1e10f和123.456F都属于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.
  • 以D或D为后缀的真正文字类型为double。例如,文字1d、1.5d、1e10d和123.456D都是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.
  • 以M或M为后缀的真正文字是十进制的。例如,字面量为1m、1.5m、1e10m和123.456M都是小数。此文字通过使用确切的值,并在必要时使用银行家的舍入将其四舍五入到最接近的可表示值,从而转换为十进制值(第4.1.7节)。文字中任何明显的比例都保留了,除非该值是圆形的或值为零(在后一种情况下,符号和刻度为0)。因此,将解析字面上的2.9 m,并以符号0、系数2900和scale 3来构成小数。

#3


7  

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

如果你的变量还不是很短,你必须明确地表示:

Object s = (Int16) 1;

#4


3  

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

没有一个是短的。只用短的s = 1;

#5


-7  

If you want to avoid type casts that often causes warnings with resharper, short.Parse("1") may also be an alternative.

如果您想避免使用resharper、short.Parse(“1”)可能也是一种替代方法。