铸造、拆箱、转换..?
最近在学习价值类型,有点困惑。此外,转换和拆箱都使用相同的语法 - (预期类型)(对象),对吗? 那么类型之间的简单转换(即强制转换还是简单转换)呢?
int x = (int)2.5; //casting?
object a=x;
int Y=(int)a; //unboxing I think
Random r=new Random();
object X=r;
Random R=(Random)X; // casting
Recently I am learning value types and I am bit confused. Also both casting and unboxing uses the same syntax - (expected type)(object), right?
And what about simple conversion between types, that is casting or just conversion?
int x = (int)2.5; //casting?
object a=x;
int Y=(int)a; //unboxing I think
Random r=new Random();
object X=r;
Random R=(Random)X; // casting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有很多事情需要考虑,但让我们先解决最简单的问题:
(type)expression
的语法是什么?嗯,就其基本形式而言,它被认为是铸造。您将表达式从一种类型转换为另一种类型。就是这样。
然而,到底会发生什么,取决于类型和很多其他因素。
如果将值类型转换为其他类型,则需要依赖所涉及的两种类型之一来声明处理此问题的转换运算符。换句话说,值类型需要定义可以转换为其他类型的转换运算符,或者其他类型需要定义可以从原始类型转换的转换运算符。
该运算符的作用取决于该运算符的作者。它是一种方法,所以它可以做任何事情。
将值类型转换为其他类型会为您提供不同的值、单独的值类型或新的引用类型,其中包含转换后的新数据。
示例:
当您将值类型转换为引用类型(通常是
object
)或值类型实现的接口之一时,装箱和拆箱就会发挥作用。示例:
当投射到接口时,拳击也会发挥作用。我们假设“someValueType”是一个结构体,它也实现了 IDisposable:
转换引用类型,也可以做其他事情。
首先,您可以定义与值类型中涉及的相同的转换运算符,这意味着将一种引用类型转换为另一种可以返回一个全新的对象,其中包含完全不同类型的信息。
转换引用类型时,装箱不会发挥作用,除非将引用类型转换回值类型(参见上文)。
示例:
或者,您可以重新解释引用,以便仍然引用有问题的同一对象,但你是通过一副不同类型的眼镜来看待它。
例子:
There's a lot of things to consider here, but let's tackle the simplest first:
What is the syntax
(type)expression
?Well, in its basic form, it is considered casting. You cast the expression from one type, to another. That's it.
However, what exactly happens, that depends on the type, and a lot of other things.
If casting a value type to something else, you depend on one of the two types involved to declare a casting operator that handles this. In other words, either the value type need to define a casting operator that can cast to that other type, or that other type need to define a casting operator that can cast from the original type.
What that operator does, is up to the author of that operator. It's a method, so it can do anything.
Casting a value type to some other type gives you a different value, a separate value type, or a new reference type, containing the new data after casting.
Example:
Boxing and unboxing comes into play when you're casting a value type to and from a reference type, typically
object
, or one of the interfaces the value type implements.Example:
Boxing also comes into play when casting to an interface. Let's assume that "someValueType" is a struct, which also implements IDisposable:
Casting a reference type, can do something else as well.
First, you can define the same casting operators that were involved in value types, which means casting one reference type to another can return a wholly new object, containing quite different type of information.
Boxing does not come into play when casting a reference type, unless you cast a reference type back to a value type (see above.)
Example:
Or, you can just reinterpret the reference, so that you still refer to the same object in question, but you are looking at it through a different pair of type glasses.
Example:
拆箱的一个重要限制是,您只能拆箱到确切的值类型(或其可为 null 的等效项),而不能拆箱到原始值类型可转换为的另一种值类型。
另一个有趣的地方是,可空值类型会被装箱为其不可为空类型,并且可以作为可空类型和不可为空类型拆箱。由于值为
null
的可空值装箱到引用null
,因此有关其类型的信息在装箱过程中会丢失。One important restriction on unboxing is that you can only unbox to the exact value-type(or its nullable equivalent) and not to another value-type the original value-type is convertible to.
Another interesting bit is that a nullable value-type gets boxed as its non nullable type, and can be unboxed as both the nullable and the non nullable type. Since a nullable that has the value
null
boxes to the referencenull
the information about its type is lost during the boxing.基本上,强制转换是一种转换形式。
您代码中的所有注释都是正确的。
不过,请务必注意引用转换与其他转换之间的区别。您显示的最终转换是引用转换 - 它维护表示身份,因此
X
和R
的值都是对同一个对象。将此与 double 到 int 转换(形式显着改变)和拆箱(将值从框内复制到变量)进行比较。
这种差异对于某些主题(例如通用方差)很重要 - 由于引用转换可用,因此仅适用于引用类型。基本上,CLR 检查所有类型是否合适,然后运行适当的代码,而无需对引用本身执行任何实际转换。
A cast is one form of conversion, basically.
All the comments in your code are correct.
It's important to note the difference between a reference conversion and other conversions, however. The final conversion you've shown is a reference conversion - it maintains representational identity, so the values of
X
andR
are both references to the same object.Compare this with a
double
toint
conversion (changes form significantly) and unboxing (copies the value from inside the box to the variable).The difference is important for some topics such as generic variance - that only works with reference types because of the reference conversions available. Basically the CLR checks that it all the types are appropriate, and then runs the appropriate code without ever having to perform any actual conversions on the references themselves.
装箱和拆箱特指将值类型转换为引用类型,然后再转换回来。查看此链接以了解更多信息装箱和拆箱(C# 编程指南)。
Boxing and Unboxing specifically refer to casting value types to reference types and back again. Check out this link for more Boxing and Unboxing (C# Programming Guide).
装箱和拆箱是由编译器在幕后完成的。因此,正确的评论是
关于铸造与转换检查这个问题:什么是转换和转换之间的区别?
Boxing and unboxing is done behind the scenes by compiler. So correct comments would be
about casting vs conversion check this question: What is the difference between casting and conversion?