Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
C# for Programmers, 2/e

© 2006
pages: ~1350

Amazon logo

This is the first in a series of three articles that shows how to declare and use generic methods in the C# programming language. In this article, we introduce the concept of generics. In Part 2 , we present an example using overloaded methods to motivate the need for generic methods. In Part 3 , we reimplement the overloaded methods from the example in Part 2 using a single generic method. These articles are intended for students who are already familiar with C# and for C# developers.
Download the code examples for this tutorial here.

[Note: This series of three articles (Part 1, Part 2, Part 3) is an excerpt (Sections 25.1–25.3) of Chapter 25, Generics, from our book C# for Programmers, 2/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C# FOR PROGRAMMERS, ©2005, pp.1115–1120. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Part 1: Introduction to Generics in C#
In Chapter 25, we presented data structures that stored and manipulated object references. You could store any object in our data structures. One inconvenient aspect of storing object references occurs when retrieving them from a collection. An application normally needs to process specific types of objects. As a result, the object references obtained from a collection typically need to be downcast to an appropriate type to allow the application to process the objects correctly. In addition, data of value types (e.g., int and double) must be boxed to be manipulated with object references, which increases the overhead of processing such data. Also, processing all data as type object limits the C# compiler's ability to perform type checking.
Though we can easily create data structures that manipulate any type of data as objects (as we did in Chapter 25), it would be nice if we could detect type mismatches at compile time-this is known as compile-time type safety. For example, if a Stack should store only int values, attempting to push a string onto that Stack should cause a compile-time error. Similarly, a Sort method should be able to compare elements that are all guaranteed to have the same type. If we create type-specific versions of class Stack class and method Sort, the C# compiler would certainly be able to ensure compile-time type safety. However, this would require that we create many copies of the same basic code.
This chapter discusses one of C#'s newest features-generics-which provides the means to create the general models mentioned above. Generic methods enable you to specify, with a single method declaration, a set of related methods. Generic classes enable you to specify, with a single class declaration, a set of related classes. Similarly, generic interfaces enable you to specify, with a single interface declaration, a set of related interfaces. Generics provide compile-time type safety. [Note: You can also implement generic structs and delegates. For more information, see the C# language specification.]
We can write a generic method for sorting an array of objects, then invoke the generic method separately with an int array, a double array, a string array and so on, to sort each different type of array. The compiler performs type checking to ensure that the array passed to the sorting method contains only elements of the same type. We can write a single generic Stack class that manipulates a stack of objects, then instantiate Stack objects for a stack of ints, a stack of doubles, a stack of strings and so on. The compiler performs type checking to ensure that the Stack stores only elements of the same type.
This chapter presents examples of generic methods and generic classes. It also considers the relationships between generics and other C# features, such as overloading and inheritance. Chapter 27, Collections, discusses the .NET Framework's generic and non-generic collections classes. A collection is a data structure that maintains a group of related objects or values. The .NET Framework collection classes use generics to allow you to specify the exact types of object that a particular collection will store.

Tutorials in This Series:
Introduction to Generics in C# (You are here.)
Motivation for Generic Methods
Generic Method Implementation

Tutorial Index