简体   繁体   English

取消装箱类对象-没有错误?

[英]Un Boxing a class object - No Error?

Probably I may be confused with boxing and unboxing. 可能我会对装箱和拆箱感到困惑。

Consider the following statement from MSDN: 考虑以下来自MSDN的声明:

"Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface." “拆箱是从类型对象到值类型或从接口类型到实现接口的值类型的显式转换。”

So, this implies that unboxing can only be applied to a value type parameter. 因此,这意味着拆箱只能应用于值类型参数。

so, this is OK. 这样就可以了

var concernedInteger = (int)myObject; //unboxing is ok.

Since class is a reference type, this should not work (because unboxing is only applicable to value type) 由于类是引用类型,因此这不起作用(因为取消装箱仅适用于值类型)

var concernedClassObject = (TestClass)testClassObject // unboxing is still ok.

My ReSharper does not show any error. 我的ReSharper没有显示任何错误。

So, my question is "How can you unbox a reference type variable when MSDN says that only value types can be unboxed" ? 因此,我的问题是“当MSDN说只能将值类型拆箱时,如何拆箱引用类型变量”?

This isn't unboxing. 这不是拆箱。 It is type casting . 它是type casting

Unboxing is pulling the value from a reference out into a value type (assuming the cast will succeed). 取消装箱会将值从引用中拉出为值类型(假设强制转换将成功)。 When you're moving reference types around like that.. it's just normal type casting. 当您像这样移动引用类型时,它只是普通的类型转换。

Boxing and Unboxing are both types of type casting anyway. 装箱和拆箱都是类型转换的type casting The reason they are given special names (boxing and unboxing) is because more goes on in a managed environment rather than just a couple of lookup tables being altered. 之所以给它们指定特殊名称(装箱和拆箱),是因为在托管环境中发生了更多的事情,而不只是更改了几个查找表。 This is why they are separated into the terms boxing and unboxing for value types.. but when it comes to reference types.. it's just type casting. 这就是为什么将它们分为值类型的装箱和拆箱这两个术语的原因。但是,对于引用类型,这只是类型转换。

what you are doing here is TypeCasting not boxing/unboxing. 您在这里所做的是TypeCasting而不是装箱/拆箱。 However the same casting syntax is used for both unboxing and explicit reference conversions 但是,拆箱和显式引用转换都使用相同的转换语法

Boxing And UnBoxing: 装箱和拆箱:

Boxing and unboxing comes into play when you're casting a value type to and from a reference type, basically object Boxingunboxing进场当你施放值类型和引用类型,基本对象

A simple boxing and unboxing example would be like 一个简单的boxingunboxing示例将是

             int i=1;
             object o=i; /// This is boxing
             int j = (int)o; /// This is unboxing

TypeCasting: 类型投放:

Typecasting causes conversion. 类型转换导致转换。 A type cast performs an explicit conversion of an expression to a given type. 类型转换将表达式显式转换为给定类型。

(type) expression

converts expression to an object of type type . expression转换为type type的对象。 Lets consider example 让我们考虑例子

 long _longval = 1;
 int i = (int)_longval

Explicit casting actually tells the compiler that we know about possible information loss but still we need to perform cast. 显式强制转换实际上告诉编译器我们知道可能的信息丢失,但是仍然需要执行强制转换。 This is ok for inbuilt Numeric types but it might happen that in reference types it is not at all compatiable .for example 对于内置的数字类型来说这是可以的,但是在引用类型中它可能根本不兼容。例如

  string _mystring="abc";
  int i=(int)_mystring;

Such casting expressions will compile successfully but they will fail at run-time giving InvalidCastException error. 这样的转换表达式将成功编译,但在运行时会失败,并给出InvalidCastException错误。

using As Keyword: 使用As关键字:

example

  MyClass _MyObject = (MyClass ) obj;
  MyClass MyObject = obj as MyClass ;

When the cast fails at first line of code an exception is thrown whereas in the second line you get only null value.Also you can use as only with reference types so for value types you have to use normal casting method only. 当强制转换在代码的第一行失败时,将引发异常,而在第二行中,您将仅获得空值。此外,您只能将与引用类型一起使用因此对于值类型,您只能使用常规的强制转换方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM