Unlocking The Power of Generics: Matt Goebel Crowe Chizek CNUG February 15, 2006
Unlocking The Power of Generics: Matt Goebel Crowe Chizek CNUG February 15, 2006
Agenda
Traditional Collections and Drawbacks Introduction to Generics Value of Generics Syntax and Samples
Traditional Collections
System.Collection namespace introduced in 1.0 version of .NET Framework Useful container types
Lists Dictionaries Hashtables CollectionBase
Drawbacks
Can control and eliminate unwanted types Create wrapper class for each type
Drawbacks
Previous example demonstrated enforcing consistent type use with 1.x Code generators such as CodeSmith can generate wrappers for you Larger code base even if it is by a code generator Still has performance overhead of boxing and unboxing
System.Collections.Generic namespace
List array dynamically sized as needed Linked list doubly linked list Queue first in, first out collection of objects Stack last in, first out collection of objects
C# Generics Sample 1
ArrayList<int> testCol = new ArrayList<int>(); for (int i = 0; i < 20; i++) { testCol.Add(i); } foreach (int someInt in testCol) { Console.WriteLine({0}, someInt); }
C# Generics Sample 2
ArrayList<Book> bookList = new ArrayList<Book>(); for (int i = 0; i < 20; i++) { bookList.Add( new Book(i) ); } foreach (Book book in bookList) { Console.WriteLine({0}, book.BookNumber); }
C++ Templates?
Classes
Public Class MyType(Of T, U) public class MyType<T,U> {
2 2 1 2
Generic Methods
Public List<I> Foo<I, J>(I val1, List<J> val2) {} Public void Foo<K, L>(K val1, List<L> val2) {}
Constraints
Can apply constraints to limit the types used for the type parameter
Public Class MyClass(Of T As {IMyClassA, IMyClassB}) End Class Public class MyClass<T> where T : IMyClassA, IMyClassB { }
Default Values
Assign a default value when the type is not known until implementation
Assigns null for reference types Assigns appropriate default value for built-in types Only available in C# for now
public class MyClass <K, V> { public V Lookup(K key) { V retVal = default(V); return retVal; } }
Nullable Types
Built-in data types such as int dont offer a good way to check if it has been assigned a value System.Nullable<T> offers a nullable type
Nullable<int> intVal = null; if (!intVal.HasValue) { Console.WriteLine("Is null"); } intVal = 0; if (intVal.HasValue) { Console.WriteLine("Has value"); }
Power Collections
Open source library of generics http://www.wintellect.com/powercollections Microsoft support and participation Additional useful types
BigList Deque OrderedDictionary OrderedSet And more
More Information
An Introduction to C# Generics http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnvs05/html/csharp_generics.asp Generics in .NET: Type Safety, Performance and Generality http://www.codeguru.com/columns/dotnet/article.php/c9661/ Power Collections - http://www.wintellect.com/powercollections/ Generics in .NET 2.0 http://www.ondotnet.com/pub/a/dotnet/2005/06/20/generics.html
Need a Job?
Were Hiring Full Time Employees that have .NET development skills Contact Tara Rodts [email protected]