View Single Post
Old Jan 17th, 2007, 11:25 AM   #4
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
Neither values are Boxed. Both i and i1 are value types on the stack. Int32 and all the other .net primatives are implemented as structs. This means they have methods and properties etc and are created on the stack. If you come from a c/c++ or java background it can be a little weird that the primatives have methods. It is one of the reason people claim that C# is a truly object oriented language because its primatives have methods.

<randomrant>
On a side note C# structs are dangerious as people make them mutable and then all sorts of funny things happen as they then treat them like they are heap allocated and referenced. The only thing they are useful for is short lived "objects" as there is less overhead in creating them as they are stack allocated.
</randomrant>

To Box an int you have to put it into a object of type Object. This will box it and put it on the heap. C# autoboxes/unboxes this so it can cause problems to novices as they dont have a clue. This is one of the reasons Java held back on autoboxing/unboxing for years because they thought it would lead to confusion and I tend to agree. Putting a cast in is not hard and it make code easier to understand but slightly more cluttered.
Tim is offline   Reply With Quote