Prev | Current Page 102 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

In Main(), there is an Integer, which is an alias for System.Int32,
and it is a value type. You could have just as well declared x as type System.Int32. The space
allocated for x is on the local stack. You then pass it as a parameter to the Print() method. The
Print() method takes an object reference and simply sends the results of calling ToString()
on that object to the console. Let??™s analyze this. Print() accepts an object reference, which is a
reference to a heap-based object. Yet you??™re passing a value type to the method. How is this
possible?
The key is a concept called boxing. At the point where a value type is defined, the CLR creates
a runtime-created wrapper class to contain the value type. Instances of the wrapper live
on the heap and are commonly called boxing objects. This is the CLR??™s way of bridging the gap
between value types and reference types.
The boxing object behaves just like any other reference type in the CLR. Also note that the
boxing type implements the interfaces of the contained value type. The boxing type is a class
type that is generated internally by the virtual execution system of the CLR at the point where
the contained value type is defined. The CLR then uses this internal class type when it performs
boxing operations as needed.


Pages:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
katalog stron szambo betonowe wierszyki dieta light katalog stron