15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

Generics overview ❘ 109<br />

foreach (int i2 in list)<br />

{<br />

Console.WriteLine(i2);<br />

}<br />

Type safety<br />

Another feature of generics is type safety. As with the ArrayList class, if objects are used, any type can be<br />

added to this collection. This example shows adding an integer, a string, <strong>and</strong> an object of type MyClass to<br />

the collection of type ArrayList:<br />

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

list.Add(44);<br />

list.Add("mystring");<br />

list.Add(new MyClass());<br />

Now if this collection is iterated using the following foreach statement, which iterates using integer<br />

elements, the compiler accepts this code. However, because not all elements in the collection can be cast to<br />

an int, a runtime exception will occur:<br />

foreach (int i in list)<br />

{<br />

Console.WriteLine(i);<br />

}<br />

Errors should be detected as early as possible. With the generic class List, the generic type T defines<br />

what types are allowed. With a definition of List, only integer types can be added to the collection.<br />

The compiler doesn’t compile this code because the Add() method has invalid arguments:<br />

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

list.Add(44);<br />

list.Add("mystring"); // compile time error<br />

list.Add(new MyClass()); // compile time error<br />

binary Code reuse<br />

Generics allow better binary code reuse. A generic class can be defined once <strong>and</strong> can be instantiated with<br />

many different types. Unlike C++ templates, it is not necessary to access the source code.<br />

As an example, here the List class from the namespace System.Collections.Generic is instantiated<br />

with an int, a string, <strong>and</strong> a MyClass type:<br />

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

list.Add(44);<br />

var stringList = new List();<br />

stringList.Add("mystring");<br />

var myClassList = new List();<br />

myClassList.Add(new MyClass());<br />

Generic types can be defined in one language <strong>and</strong> used from any other .<strong>NET</strong> language.<br />

Code bloat<br />

How much code is created with generics when instantiating them with different specific types<br />

Because a generic class definition goes into the assembly, instantiating generic classes with specific types<br />

doesn’t duplicate these classes in the IL code. However, when the generic classes are compiled by the JIT<br />

compiler to native code, a new class for every specific value type is created. Reference types share all the<br />

same implementation of the same native class. This is because with reference types, only a 4-byte memory<br />

address (with 32-bit systems) is needed within the generic instantiated class to reference a reference type.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!