Chapter 18: Generics


Of the many new features added by C# 2.0, the one that has the most profound impact is generics. Not only did it add a new syntactical element to C#, but it also caused many additions to the core API. Through the use of generics, it is possible to create classes, interfaces, and methods that will work in a type-safe manner with various kinds of data. Many algorithms are logically the same no matter what type of data they are being applied to. For example, the mechanism that supports a stack is the same whether that stack is storing items of type int, string, object, or a user-defined class. With generics, you can define an algorithm once, independently of any specific type of data, and then apply that algorithm to a wide variety of data types without any additional effort.

This chapter describes the syntax, theory, and use of generics. It also shows how generics provide type safety for some previously difficult cases. Once you have completed this chapter, you will want to examine Chapter 23, which covers collections. There you will find many examples of generics at work in the new generic collection classes.

What Are Generics?

At its core, the term generics means parameterized types. Parameterized types are important because they enable you to create classes, interfaces, methods, and delegates in which the type of data operated on is specified as a parameter. Using generics, it is possible to create a single class, for example, that automatically works with different types of data. A class, interface, method, or delegate that operates on a parameterized type is called generic, as in generic class or generic method.

It is important to understand that C# has always given you the ability to create generalized classes, interfaces, methods, and delegates by operating through references of type object. Because object is the base class of all other classes, an object reference can refer to any type object. Thus, in pre-generics code, generalized code used object references to operate on a variety of different types of objects. The problem was that it could not do so with type safety.

Generics add the type safety that was lacking. They also streamline the process because it is no longer necessary to employ casts to translate between object and the type of data that is actually being operated upon. Thus, generics expand your ability to reuse code and let you do so safely and easily.

Note 

A Warning to C++ and Java Programmers: Although C# generics are similar to templates inC++ and generics in Java, they are not the same as either. In fact, there are some fundamental differences between these three approaches to generics. If you have a background in C++ or Java, it is important to not jump to conclusions about how generics work in C#.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net