Generics, Collections, and Generic Patterns
With generics (e.g., List, MyClass) you can write code once and use it many times for different types of data. (Generics are one of the building blocks of LINQ) Moreover, since C# 2, the compiler even lets you skip the type parameters if it can infer the types from other arguments. - -- -- - Generics make development easier because you write one piece of code to handle many types of data, and the code is still type-safe and maintains both compiler and intellisense support. - -- -- - Generics abstract type information and also make that generic type information available to use. Generics also promote re-use. ... We will use many useful .NET collection classes (List, Dictionary, Stack, Queue, LinkedList, etc.), and we’ll consider strategies for deciding which to use. In addition to generic collections, we will write our own generic algorithms and cover some strategies, guidelines, and patterns for generic algorithms. - -- -- - We will cover the IEnumerable interface (which is required for “foreach( )”, LINQ, etc.), and how to *easily* implement IEnumerable by having the compiler do most of the work. - -- -- - We will see all of this and more in operation using a simple Poker card example: from a card class, we can easily build card decks and poker operations using generic collections, methods, and delegates. We will generate, shuffle, and deal hands of cards (with and without cheating), and evaluate poker hands by referencing a collection of cards. - -- -- - If there is time, we will see some examples of useful code that uses both generics and reflection.
Tags: Generics | Design Patterns | Collections | C# | Architecture