在C#中将char隐式转换为int [英] Implicit type cast of char to int in C#

查看:147
本文介绍了在C#中将char隐式转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对隐式类型转换有疑问

I have a question about the implicit type conversion

为什么这种隐式类型转换在C#中起作用?我知道隐式代码通常不起作用。

Why does this implicit type conversion work in C#? I've learned that implicit code usually don't work.

我这里有一个有关隐式类型转换的代码示例

I have a code sample here about implicit type conversion

 char c = 'a';
 int x = c;
 int n = 5;
 int answer = n * c;
 Console.WriteLine(answer);


推荐答案

铸造会导致数据丢失。此处 char 是16位,而 int 是32位。

Casting will cause data loss. Here char is 16 bit and int is 32 bit. So the cast will happen without loss of data.

现实生活中的例子:在没有外部帮助的情况下,我们可以将小型船只放入大型船只,反之亦然。

Real life example: we can put a small vessel into a big vessel but not vice versa without external help.

这篇关于在C#中将char隐式转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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