C#数值转换-隐式数值转换表参考
- 作者: 用户65077202
- 来源: 51数据库
- 2021-08-23
什么是隐式转换
implicit conversion
隐式转换就是直接使用,比如可以把一个 byte 类型直接用在 int 上。
比如以下直接把 byte 的 b 赋给 int 的 n,之间是直接用的,没存在什么额外的关键字,全由系统自动完成类型转换。
byte b = 1;
int n = b;
隐式数值转换表(摘自 msdn)
备注(摘自 msdn)
从 int、uint 或 long 到 float 的转换以及从 long 到 double 的转换的精度可能会降低,但数值大小不受影响。
不存在到 char 类型的隐式转换。
不存在浮点型与 decimal 类型之间的隐式转换。
int 类型的常数表达式可转换为 sbyte、byte、short、ushort、uint 或 ulong,前提是常数表达式的值处于目标类型的范围之内。
implicit conversion
隐式转换就是直接使用,比如可以把一个 byte 类型直接用在 int 上。
比如以下直接把 byte 的 b 赋给 int 的 n,之间是直接用的,没存在什么额外的关键字,全由系统自动完成类型转换。
复制代码 代码如下:
byte b = 1;
int n = b;
隐式数值转换表(摘自 msdn)
|
从 |
到 |
|---|---|
|
sbyte |
short、int、long、float、double 或 decimal |
|
byte |
short、ushort、int、uint、long、ulong、float、double 或 decimal |
|
short |
int、long、float、double 或 decimal |
|
ushort |
int、uint、long、ulong、float、double 或 decimal |
|
int |
long、float、double 或 decimal |
|
uint |
long、ulong、float、double 或 decimal |
|
long |
float、double 或 decimal |
|
char |
ushort、int、uint、long、ulong、float、double 或 decimal |
|
float |
double |
|
ulong |
float、double 或 decimal |
备注(摘自 msdn)
从 int、uint 或 long 到 float 的转换以及从 long 到 double 的转换的精度可能会降低,但数值大小不受影响。
不存在到 char 类型的隐式转换。
不存在浮点型与 decimal 类型之间的隐式转换。
int 类型的常数表达式可转换为 sbyte、byte、short、ushort、uint 或 ulong,前提是常数表达式的值处于目标类型的范围之内。
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
