我知道像 1.5
这样的东西是 double 的,所以你需要使用 m
后缀将其分配给小数。
decimal d = 1.5; // won't compile
decimal d = 1.5m; // will compile
现在,像 0
这样的文字是一个 int。但是为什么我只能把它分配给一个long呢?将 0
与 0L
分配给 long 类型的变量之间有什么区别吗?
long l = 0;
long l = 0L;
最佳答案
But why can I just assign it to a long?
11.2.3 the spec状态:
Implicit numeric conversions
The implicit numeric conversions are:
• From sbyte to short, int, long, float, double, or decimal.
• From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.
• From short to int, long, float, double, or decimal.
• From ushort to int, uint, long, ulong, float, double, or decimal.
• From int to long, float, double, or decimal.
• From uint to long, ulong, float, double, or decimal.
• From long to float, double, or decimal.
• From ulong to float, double, or decimal.
• From char to ushort, int, uint, long, ulong, float, double, or decimal.
• From float to double.
Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.
从 int 到 long、float、double 或 decimal。
是此处的相关项。 0默认是int
,所以可以隐式转换成long
。
Is there any difference between assigning 0 versus 0L to a variable of type long?
它们的行为完全相同。 IL 完全相同。
关于c# - 什么时候需要数字后缀 L(长)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61517324/