简体   繁体   中英

Object type and boxing

MSDN says that

The object data type is the type to and from which objects are boxed.

I thought only value types are boxed? Now I am bit confused. So when I assign reference type variable to object variable, its boxing?

It just states that System.Object is the type used when any object is boxed.

In other words, when a value type is boxed, it is boxed within System.Object type.

Only value types are boxed.

So, to be accurate the sentence should be:

"The object data type is the type to and from which values are boxed."

Yes only value types are boxed. Boxing a value type packages it inside an instance of the Object reference type. What this statement says is that it is System.Object which is used to box/unbox value types to and from.

Boxing is only done on value types. I'm wondering though why MSDN doesn't state it more precisely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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