简体   繁体   English

转换为ValueType时的C#装箱

[英]C# boxing when casting to ValueType

I was reading about the ValueType class and I am wondering if, when something gets casted to a ValueType, whether it is getting boxed? 我正在阅读有关ValueType类的信息 ,我想知道是否在将某些内容转换为ValueType时是否装箱了? Example: 例:

void DoSomething(ValueType valueType)
{
}

DoSomething(5);

Did the int represented by the literal 5 get boxed when received by DoSomething method? 当由DoSomething方法接收时,由文字5表示的int是否装箱了?

Yes, it gets boxed. 是的,它装箱了。

Think about it... for the value not to get boxed there should be some common binary representation that can be any value type - including all built in ones and any struct you may define in the future. 考虑一下...为了使该值不会被装箱,应该有一些通用的二进制表示形式,该表示形式可以是任何值类型-包括所有内置值和将来可能定义的任何结构。

Since such a binary representation doesn't exist the value must be boxed. 由于这样的二进制表示形式不存在,因此必须将值装箱。

Explanation: 说明:

When you call a method with parameters the caller places a sequence of bits at an agreed about location and in an agreed about format, for example an int is 32bits with negative numbers encoded as 1-complement, a double is 64bits encoded in IEEE floating point format, etc. 当您调用带有参数的方法时,调用方将一系列位放在约定的位置和格式上,例如,int是32位,负数编码为1补码,double是64位,以IEEE浮点编码格式等

You can't have one method that can except both unboxed int and double because it wouldn't know how many bits to read and how to decode themץ 您没有一种方法可以将unboxed int和double除外,因为它不知道读取多少位以及如何解码它们decode

If you do want a method to accept both you can give the function the memory location of the value (the location itself is of known size and format so the method knows how to decode it) and some meta data so the method knows the actual type of the value - wrapping the value with metadata and providing it's memory location is called (surprise, surprise) "boxing" 如果您确实希望方法接受两者,则可以为函数提供值的存储位置(该位置本身的大小和格式已知,因此该方法知道如何对其进行解码)和一些元数据,从而使该方法知道实际的类型。的价值-用元数据包装价值并提供其存储位置被称为(惊奇)“装箱”

So, anytime you pass a value using a parameter/variable/whatever that is not the exact type the system has to box the value or the receiver wouldn't know much memory the value really uses and how to decode that memory from a sequence of bits back to a number or structure. 因此,无论何时您使用参数/变量/传递的值不是系统必须将其装箱的确切类型,接收者都不知道该值真正使用了多少内存,以及如何从序列中解码该内存位返回数字或结构。

This only applies to value types because reference types are always passed by using the memory location (the memory location is called a "reference" in .net). 这仅适用于值类型,因为引用类型始终通过使用内存位置来传递(该内存位置在.net中称为“引用”)。

Yes it does, ValueType is a class (and thus a reference type so will incur boxing). 是的, ValueType是一个类(因此是引用类型,因此会引起装箱)。

This question covers similar ground: 这个问题涉及类似的理由:

Why Enum's HasFlag method need boxing? 为什么Enum的HasFlag方法需要装箱?

According to Marc Gravell's comment in the MSDN article you link to it is. 根据您在MSDN文章中链接的Marc Gravell的评论。

it should be stressed that while ValueType can be used to restrict values to value-types, casting to ValueType (implicitly or explicitly) is still a boxing operation; 应该强调的是,虽然可以使用ValueType将值限制为值类型,但是(隐式或显式)强制转换为ValueType仍然是一种装箱操作; only the concrete known value-types ("DateTime", "int", etc) can be handled directly as value-types - ValueType itself is treated as a class (so boxing). 仅具体的已知值类型(“ DateTime”,“ int”等)可以直接作为值类型处理-ValueType本身被视为类(因此装箱)。

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

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