15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

108 ❘ ChaPTer 5 Generics<br />

The following sections explore the advantages <strong>and</strong> disadvantages of generics, particularly in regard to:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Pe r f o r m a n c e<br />

Type safety<br />

Binary code reuse<br />

Code bloat<br />

Performance<br />

Naming guidelines<br />

One of the big advantages of generics is performance. In Chapter 10, “ Collections, ” you will see non - generic<br />

<strong>and</strong> generic collection classes from the namespaces System.Collections <strong>and</strong> System.Collections.<br />

Generic . Using value types with non - generic collection classes results in boxing <strong>and</strong> unboxing when the<br />

value type is converted to a reference type <strong>and</strong> vice versa.<br />

Boxing <strong>and</strong> unboxing is discussed in Chapter 7, “ Operators <strong>and</strong> Casts. ” Here is just a<br />

short refresher about these terms.<br />

Value types are stored on the stack. Reference types are stored on the heap. <strong>C#</strong> classes are reference types;<br />

structs are value types. .<strong>NET</strong> makes it easy to convert value types to reference types, so you can use a<br />

value type everywhere an object (which is a reference type) is needed. For example, an int can be assigned<br />

to an object. The conversion from a value type to a reference type is known as boxing . Boxing happens<br />

automatically if a method requires an object as a parameter, <strong>and</strong> a value type is passed. On the other side, a<br />

boxed value type can be converted to a value type by using unboxing. With unboxing, the cast operator<br />

is required.<br />

The following example shows the ArrayList class from the namespace System.Collections . ArrayList<br />

stores objects; the Add() method is defi ned to require an object as a parameter, <strong>and</strong> so an integer type is<br />

boxed. When the values from an ArrayList are read, unboxing occurs when the object is converted to<br />

an integer type. This may be obvious with the cast operator that is used to assign the fi rst element of the<br />

ArrayList collection to the variable i1 , but also happens inside the foreach statement where the variable<br />

i2 of type int is accessed:<br />

var list = new ArrayList();<br />

list.Add(44); // boxing — convert a value type to a reference type<br />

int i1 = (int)list[0]; // unboxing — convert a reference type to<br />

// a value type<br />

foreach (int i2 in list)<br />

{<br />

Console.WriteLine(i2); // unboxing<br />

}<br />

Boxing <strong>and</strong> unboxing are easy to use but have a big performance impact, especially when iterating through<br />

many items.<br />

Instead of using objects, the List < T > class from the namespace System.Collections.Generic allows you<br />

to defi ne the type when it is used. In the example here, the generic type of the List < T > class is defi ned as<br />

int , so the int type is used inside the class that is generated dynamically from the JIT compiler. Boxing <strong>and</strong><br />

unboxing no longer happens:<br />

var list = new List < int > ();<br />

list.Add(44); // no boxing — value types are stored in the List < int ><br />

int i1 = list[0]; // no unboxing, no cast needed<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!