代码之家  ›  专栏  ›  技术社区  ›  comecme

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

c#
  •  0
  • comecme  · 技术社区  · 4 年前

    我知道有点像 1.5 是双人床,所以你需要用 m 后缀将其指定为小数点。

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

    现在,字面意思是 0 是int。但是为什么我可以把它赋给long呢?分配和分配有什么区别吗 0L 一个long类型的变量?

    long l = 0;
    long l = 0L;
    
    0 回复  |  直到 4 年前
        1
  •  3
  •   mjwills Myles McDonnell    4 年前

    但是为什么我可以把它分配给一个长的?

    第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.
    

    From int to long, float, double, or decimal. int 默认情况下,它可以隐式转换为 long .

    将0和0L赋给变量