12.07.2015 Views

Accelerated

Accelerated

Accelerated

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ CLASSES, STRUCTS, AND OBJECTS 93// must box the valueIPrint printer = myval;printer.Print();}}As another example, consider that the System.Int32 type supports the IConvertible interface.However, most of the IConvertible interface methods are implemented explicitly. Therefore, even ifyou want to call an IConvertible method, such as IConvertible.ToBoolean on a simple int, youmust box it first.■Note Typically, you want to rely upon the external class System.Convert to do a conversion like the one mentionedpreviously. I only mention calling directly through IConvertible as an example.Efficiency and ConfusionAs you might expect, boxing and unboxing are not the most efficient operations in the world. What’sworse is that the C# compiler silently does the boxing for you. You really must take care to knowwhen boxing is occurring. Unboxing is usually more explicit, since you typically must do a castoperation to extract the value from the box, but there is an implicit case I’ll cover soon. Either way,you must pay attention to the efficiency aspect of things. For example, consider a container type,such as a System.Collections.ArrayList. It contains all of its values as references to type object. Ifyou were to insert a bunch of value types into it, they would all be boxed! Thankfully, generics,which were introduced in C# 2.0 and .NET 2.0 and are covered in Chapter 10, can solve this inefficiencyfor you. However, note that boxing is inefficient and should be avoided as much as possible.Unfortunately, since boxing is an implicit operation in C#, it takes a keen eye to find all of the casesof boxing. The best tool to use if you’re in doubt whether boxing is occurring or not is ILDASM.Using ILDASM, you can examine the IL code generated for your methods, and the box operationsare clearly identifiable. You can find ILDASM.exe in the .NET SDK \bin folder.As mentioned previously, unboxing is normally an explicit operation introduced by a cast fromthe boxing object reference to a value of the boxed type. However, unboxing is implicit in onenotable case. Remember how I talked about the differences in how the this reference behaveswithin methods of classes vs. methods of structs? The main difference is that, for value types, thethis reference acts as either a ref or an out parameter, depending on the situation. So when you calla method on a value type, the hidden this parameter within the method must be a managed pointerrather than a reference. The compiler handles this easily when you call directly through a value-typeinstance. However, when calling a virtual method or an interface method through a boxedinstance—thus, through an object—the CLR must unbox the value instance so that it can obtain themanaged pointer to the value type contained within the box. After passing the managed pointer tothe contained value type’s method as the this pointer, the method can modify the fields throughthe this pointer, and it will apply the changes to the value contained within the box. Be aware ofhidden unboxing operations if you’re calling methods on a value through a box object.■Note Unboxing operations in the CLR are not inefficient in and of themselves. The inefficiency stems from thefact that C# typically combines that unboxing operation with a copy of the value.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!