Char将int隐式转换为“幕后" [英] Char to int implicit cast Behind the Scenes
问题描述
以下内容在c#中有效,因为Char
可以隐式转换为int
The following is valid in c# as Char
can be implicitly cast to int
int i = 'a';
我只是对.Net Framework的幕后工作感到好奇,我查看了char
和int
类型的源代码,但找不到它的写法.
i am just curious about what .Net Framework do behind the scenes, i looked in to char
and int
types source code but unable to find where it's written.
任何人都可以解释幕后发生的事情吗?
Can any body explain what happens behind the scene?
推荐答案
在C#中,我们有一种称为单根统一类型系统的东西.这意味着每个现有类型都是一个根类型的子类型. C#中的Object
.因此,char
或int
仅是System.Char
和System.Int32
的简称(别名).从无类型的Object
到long
或char
(值类型)的转换称为(un)boxing
.
In C# we have something that is called a Single-Rooted unified type system. That means every existing type is a subtype of one root type. Object
in C#. So char
or int
is only short (alias) for System.Char
and System.Int32
. The conversion from an untyped Object
to long
or char
(value types) is called (un)boxing
.
与 Java 相比,这是一个真正的区别,在Java中,原始类型为int
,char
..并且存在Object
.像Int
这样的Java类都包装原始类型. https://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
This is a real difference compared to Java where there are the primitive types int
,char
.. and there is Object
. Java classes like Int
are wrapping the primitive types. https://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
因为在C#中,所有内容都是Object
(我是对象,因为我们很多!),所以所有值类型都实现了IConvertible
,正如其他人所说的那样.
Because in C# everything is an Object
(I am Object, for we are many!), all the value types implement IConvertible
, as the others said.
还有一个在内部使用最多的枚举TypeCode
,它是分配给值类型对象(如int
)的运行时反射信息. Convert.To
方法正在使用这种类型的信息来完成工作.
And there is a mostly internally used enum TypeCode
that is the runtime reflection info that is assigned to value type object´s like int
. The Convert.To
method´s are using this type information to do their job.
http://referencesource.microsoft.com/#mscorlib/system/convert. cs
mscorlib(系统名称空间)可以在github上的下找到https://github.com/dotnet/coreclr/tree/master/src/mscorlib/src/System
The mscorlib (System namespace) can be found on github under https://github.com/dotnet/coreclr/tree/master/src/mscorlib/src/System
此外,因为值类型是完整的对象(结构),所以在运行时可以使用许多类型/元信息.
Further because value types are full objects (struct) lots of type/meta information is available during runtime.
例如,与C ++相比,C#是一种安全的语言.它不允许不安全的强制转换.
C# is a safe(er) language compared to c++ for example. It does not allow unsafe impilict casts.
从char
到int
的转换是widening
. Int
比char
更大/具有更多空间.铸造是安全的.
The cast from char
to int
is a widening
. Int
is bigger/has more space than char
. It´s safe to cast.
但这就是为什么它与Joanvo所写的(int) 'a'
不同的原因.
这是一个明确的强制转换,无论是否安全,您都在强迫.Net进行操作.
But that´s why it is not the same as (int) 'a'
like Joanvo wrote.
This is an explicit cast and you are forcing .Net to do it, no matter it´s safe or not.
对于int i = 'a';
,由于范围扩大,可以执行隐式强制转换.
但是,如果尝试使用char c = 42;
Visual Studio,则编译器将不允许这样做.除非您在明确意识到可能不是int
的所有信息都将适合char
For int i = 'a';
it´s fine to do an implicit cast because of the widening.
But if you try char c = 42;
Visual Studio and the compiler will not allow it. Unless you force it with an explicit cast while being aware that maybe not all information from the int
will fit into the char
这也是为什么您不能在 if 中使用不同于布尔(表达式)的东西的原因.
This is also the reason why you can not use something different than a boolean (expression) inside an if.
在C/C ++中,您可以编写if(1) { }
或if(0) {}
.这在C#中是不允许的,您必须在其中编写if(..==1) {}
.在C/C ++中,这通常用于检查 null 的指针.
In C/C++ you can write if(1) { }
or if(0) {}
. That is not allowed in C#, where you have to write if(..==1) {}
. In C/C++ this is often used to check pointer for null.
您可以为自己的类型重载implicit
和explicit
运算符,这有时很有用.至少知道有可能有用.
https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx
You can overload the implicit
and explicit
operator for own types, which is useful sometimes. At least useful to know it is possible.
https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx
这篇关于Char将int隐式转换为“幕后"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!