CHAPTER 17

image

Generic Types

It is sometimes useful to separate the implementation of a class—the members and methods that it exposes—from the type it is using. A list of items, for example, behaves in the same way whether it is a list of Decimal items or a list of Employee items.

A generic type is used to create such an implementation. The word generic refers to the implementation being written using a generic type rather than a specific one.

A List of Integers

Consider the following class that stores integer values:

public class IntList{    int m_count = 0;    int[] m_values;    public IntList(int capacity)    {       m_values = new int[capacity]; ...

Get A Programmer's Guide to C# 5.0, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.