c# - 什么时候需要数字后缀 L(长)?

标签 c#

我知道像 1.5 这样的东西是 double 的,所以你需要使用 m 后缀将其分配给小数。

decimal d = 1.5; // won't compile
decimal d = 1.5m; // will compile

现在,像 0 这样的文字是一个 int。但是为什么我只能把它分配给一个long呢?将 00L 分配给 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/

相关文章:

c# - 只要没有单击任何项​​目,就阻止 ToolStripDropDownButton 关闭

c# - 如何检查 bitArray 包含任何真值或任何假值?

c# - C#在UDP上接收到的数据总是无用的

c# - 工作单元模式的泛型问题

c# - C# 类型系统是否可靠且可判定?

c# - #region 描述在 .net 中编译成 .exe?

c# - 如何只允许每个用户一个 session

c# - 统一 3D。子对象没有改变

c# - 从 EF 核心获取实体而不加载所有字段

c# - 通过 MySQL 更新时出现错误 输入字符串格式不正确