0% found this document useful (0 votes)
75 views27 pages

Unlocking The Power of Generics: Matt Goebel Crowe Chizek CNUG February 15, 2006

This document discusses generics in .NET. It begins with an overview of traditional collections and their drawbacks, such as lack of type safety. It then introduces generics as a way to provide type safety. Examples are given showing how generics eliminate boxing and unboxing overhead. The document also discusses creating custom generic classes and methods, applying constraints, default values, and powerful third party collections like Power Collections.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views27 pages

Unlocking The Power of Generics: Matt Goebel Crowe Chizek CNUG February 15, 2006

This document discusses generics in .NET. It begins with an overview of traditional collections and their drawbacks, such as lack of type safety. It then introduces generics as a way to provide type safety. Examples are given showing how generics eliminate boxing and unboxing overhead. The document also discusses creating custom generic classes and methods, applying constraints, default values, and powerful third party collections like Power Collections.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

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

Sample Class Definition


class Book { private int bookNumber = 0; public int BookNumber { get { return this.bookNumber ; } set { this. bookNumber = value; } } public Book( int _bookNumber ) { this.bookNumber = _bookNumber; } }

Sample ArrayList Use


ArrayList bookList = new ArrayList(); for( int i = 0; i < 20; i++) { bookList.Add( new Book(i) ); } IEnumerator listEnum = bookList.GetEnumerator(); while( listEnum.MoveNext() ) { Console.WriteLine({0}, ((Book)(listEnum.Current)).BookNumber); }

Drawbacks

No type checking enforcement at compile time


Doesnt prevent adding unwanted types Can lead to difficult to troubleshoot issues Runtime errors!

All items are stored as objects


Must be cast going in and coming out Performance overhead of boxing and unboxing specific types

Enforcing Consistent Types

Can control and eliminate unwanted types Create wrapper class for each type

Enforcing Consistent Types


private class BookCollection : CollectionBase { public Book this[ int index ] { get { return (Book) List[index] ;} set { List[index] = value; } }

public int Add( Book.BookNumber ) { return List.Add(BookNumber); }


public void Remove( Book.BookNumber ) { List.Remove( BookNumber ); } }

Enforcing Consistent Types


BookCollection bookCol = new BookCollection (); for( int i = 0; i < 20; i++) { bookCol.Add( new Book(i) ); } IEnumerator listEnum = bookCol.GetEnumerator(); while( listEnum.MoveNext() ) { Console.WriteLine({0}, ((Book)(listEnum.Current)).BookNumber); }

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

The Generic Solution

Open constructed types


Classes defined without a specific type Type is specified when instantiated

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

The Generic Solution Continued

Provides type safety at compile time

No loss of performance or code bloat

The .NET CLR recognizes Generics


Only one definition of a parameterized data structure, no matter how many types are used Affects Microsoft Intermediate Language (MSIL)

VB.NET Generics Sample 1


Dim testCol As New List(Of int32)() For i as int32 = 0 To 19 testCol.Add(i) Next For each someInt As int32 in testCol Console.WriteLine({0}, someInt) Next

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); }

VB.NET Generics Sample 2


Dim bookList As New List(Of Book)() For i as int32 = 0 To 19 bookList.Add(New Book(i)) Next For each book As Book in bookList Console.WriteLine({0}, book.BookNumber) Next

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?

Generics are NOT equal to Templates Compile-Time vs. Run-Time


Templates = Compile-Time Generics = Run-Time

Code Bloat? Pros and Cons for each

Create Your Own Generics

Generics can be applied a number of ways

Classes
Public Class MyType(Of T, U) public class MyType<T,U> {

Method return types and parameters


Public Function Foo() As T public T Foo()

Fields and properties Indexers, properties, events, structs, interfaces, etc.

Example of Create Your Own Generic


public class MyGeneric<T> { private static int objectCount = 0; public MyGeneric() { objectCount++; } public int Count { get { return objectCount; } } public T SomeMethod() }

Example of Create Your Own Generic


Class SomeApplication { static void Main(string[] args) { MyGeneric<int> myIntGeneric = new MyGeneric<int>(); MyGeneric<int> myIntGeneric2 = new MyGeneric<int>(); int someInt = myIntGeneric.SomeMethod(); MyGeneric<Book> myBookGeneric = new MyGeneric<Book>(); Book someBook = myBookGeneric.SomeMethod(); Console.WriteLine(myIntGeneric.Count); Console.WriteLine(myIntGeneric2.Count); Console.WriteLine(myBookGeneric.Count); Console.WriteLine(new MyGeneric<Book>().Count); } }

2 2 1 2

Generic Methods

Can overload generic methods


Method signatures can be a challenge Type parameters influence signature Return type does not influence signature Examples

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]

You might also like