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.

110 ❘ ChaPTer 5 Generics<br />

Value types are contained within the memory of the generic instantiated class, <strong>and</strong> because every value type<br />

can have different memory requirements, a new class for every value type is instantiated.<br />

naming guidelines<br />

If generics are used in the program, it helps when generic types can be distinguished from non-generic types.<br />

Here are naming guidelines for generic types:<br />

➤ Generic type names should be prefixed with the letter T.<br />

➤<br />

If the generic type can be replaced by any class because there’s no special requirement, <strong>and</strong> only one<br />

generic type is used, the character T is good as a generic type name:<br />

public class List { }<br />

➤<br />

public class LinkedList { }<br />

If there’s a special requirement for a generic type (for example, it must implement an interface or<br />

derive from a base class), or if two or more generic types are used, descriptive names should be used<br />

for the type names:<br />

public delegate void EventH<strong>and</strong>ler(object sender, TEventArgs e);<br />

public delegate TOutput Converter(TInput from);<br />

public class SortedList { }<br />

CreaTing generiC Classes<br />

First start with a normal, non-generic simplified linked list class that can contain objects of any kind, <strong>and</strong><br />

later convert this class to a generic class.<br />

With a linked list, one element references the next one. So, you must create a class that wraps the<br />

object inside the linked list <strong>and</strong> references the next object. The class LinkedListNode contains a<br />

property named Value that is initialized with the constructor. In addition to that, the LinkedListNode<br />

class contains references to the next <strong>and</strong> previous elements in the list that can be accessed from<br />

properties.<br />

public class LinkedListNode<br />

{<br />

public LinkedListNode(object value)<br />

{<br />

this.Value = value;<br />

}<br />

public object Value { get; private set; }<br />

}<br />

public LinkedListNode Next { get; internal set; }<br />

public LinkedListNode Prev { get; internal set; }<br />

code snippet LinkedListObjects/LinkedListNode.cs<br />

The LinkedList class includes First <strong>and</strong> Last properties of type LinkedListNode that mark the<br />

beginning <strong>and</strong> end of the list. The method AddLast() adds a new element to the end of the list. First,<br />

an object of type LinkedListNode is created. If the list is empty, the First <strong>and</strong> Last properties are<br />

set to the new element; otherwise, the new element is added as the last element to the list. By<br />

implementing the GetEnumerator() method, it is possible to iterate through the list with the<br />

foreach statement. The GetEnumerator() method makes use of the yield statement for creating an<br />

enumerator type.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!