为什么C#不要求转换长增加一倍显式转换? [英] Why C# does not require explicit casting for convert Long To Double?

查看:135
本文介绍了为什么C#不要求转换长增加一倍显式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,我的英语不好。
我的代码片段:

At first, sorry for my bad english. I have fragment of code:

long x = 9223372036854775807L;
double f = x;
Console.WriteLine(x);           
Console.WriteLine(f);

输出是:

9223372036854775807
9,22337203685478E+18

我没有变任何错误,在编译和执行这些代码。
我们有精度损失,而长转换为双。
为什么C#不要求在这种情况下显式转换?

I'm not getting any errors while compiling and execution this code. We have a loss of precision while converting Long to Double. Why C# does not require explicit casting in that case ?

谢谢大家。

推荐答案

语言有内置的一些隐式转换。

The language has some implicit conversion built into it.

下表是从文档,这就是为什么你被允许没有显式类型转换或转换指定值:

The following table is from the documentation, which is why you are allowed to assign the value without an explicit cast or conversion:

From        To
===============================================================================
sbyte       short , int, long, float, double, or decimal
byte        short , ushort, int, uint, long, ulong, float, double, or decimal
short       int , long, float, double, or decimal
ushort      int , uint, long, ulong, float, double, or decimal
int         long , float, double, or decimal
uint        long , ulong, float, double, or decimal
long        float , double, or decimal
char        ushort , int, uint, long, ulong, float, double, or decimal
float       double
ulong       float , double, or decimal

和文档中其国家(重点煤矿):

And in the documentation it states (emphasis mine):

的精度,但幅度可能在会丢失从
INT,UINT,长或ulong的转换,从长期浮动和或ULONG翻番

Precision but not magnitude might be lost in the conversions from int, uint, long, or ulong to float and from long or ulong to double.

这篇关于为什么C#不要求转换长增加一倍显式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆