开发者

How is an "int" assigned to an object?

How are we able to assig开发者_如何学JAVAn an integer to an object in .NET?

Reference types are derived from System.Object and value types are from System.ValueType.

So, how is it possible?


The term "boxing" is very opaque, but it is easy to visualize what is actually going on by using the debugger. Write a little console mode application like this:

using System;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            int value = 42;
            object obj = value;
        }  // <== Breakpoint here
    }
}

Set a breakpoint where indicated and press F5. When the breakpoint hits, use Debug + Windows + Memory + Memory 1. In the Address box, type "obj". You'll get a hex dump of the memory content of the object. Right-click the window and select "4-byte Integer", the best way to visualize the object in this case. You'll see something resembling this:

0x01CBF6BC  6e1a2d34 0000002a

The interesting parts here are 0x01CBF6BC, that's the address of the object on the garbage collected heap. The next hex number, 6e1a2d34 is the so-called "type handle", also known as the "method table pointer". That a 'cookie' that identifies the type of the object. System.Int32 in this case. Very important, it will be used later when the object is unboxed back to an Int32 to verify that the boxed value is actually an integer.

The next value you see, 0000002a, is the value of the boxed object. You can use Windows calculator in programmer mode to convert back to decimal, it is 42.

Experiment with this, using different values and different types to see how it affects the boxed object. You can modify the hex and see what effect it has on the obj value as displayed by the debugger.

The hex dump I gave you was for a 4 byte boxed value, boxing a double will take 8 bytes. Boxing a struct will take more bytes. There's also a part of the object header you can't see, the so-called syncblock, located at the address - 4. Try the lock statement to see that changing.


This is done by the process of boxing and unboxing. Please see following links for further reference:

Boxing and Unboxing (C# Programming Guide)
Boxing and Unboxing of Value Types : What You Need to Know?
What is the difference between boxing/unboxing and type casting?


Boxing and Unboxing:

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.


You are boxing the integer in an object.

That is, an object is created that wraps (or boxes) the integer. Kind of like putting something in a box in real life.


If you look at System.ValueType, it too derives from System.Object

Also see How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes


Try this method:

object ob;
int i=10;
ob=i;//(boxing)
int b;
b=(int)ob;//(unboxing)


You should read up on boxing and unboxing in c# which should tell you they how/why.

This link has a great explanation and explains the reference vs value type question you are asking about:

http://www.dijksterhuis.org/exploring-boxing/

Boxing is as simply putting a basic type in wrapper (making it a fully blown object), and unboxing taking that wrapped object and converts it back to a simpler type. To do the boxing managed memory needs to be allocated on the heap, references need to be updated, and the contents of the value type have to be copied.


int in .NET has an underlying Integer type object and an implicit conversion.


System.ValueType is also derived from System.Object, so that is why:

See inheritance hierarchy here:

http://msdn.microsoft.com/en-us/library/system.valuetype.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜