Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

An Overview of Generics


Untitled

One of the coolest new arrows in the .NET developer's quiver is generics. When Microsoft introduced C# a few years ago, many developers complained because the language, for many aspects similar to C++ and Java, lacked any form of support for generics. Generics get the spotlight at last with the .NET Framework 2.0. But what are generics, anyway?

Generics essentially enable you to define parameters on your type definitions. For example, a list is an entity that virtually all software frameworks and general-purpose libraries render as a type. And with good reason. But a list of what? A list maps to a type because it can conveniently be used to represent collections of homogeneous or heterogeneous data. But again, a list of what? It's clear that with types representing multi-values entities, the canonical definition of type is not sufficient. So a list is a sort of parametric type. Generics are just the language tools to express parametric types.

Languages that don't support generics force you to have lists of the most generic type they know-typically, Object. When you have a list of generic objects, you also start having potential problems.

First and foremost, types can't be fully checked at compile time. Any compiler will let you store a ProductInfo object in a list and use it wherever a double is expected. The genericity of the Object type hides all these programming issue and you won't find it out until it runs. The second down side is boxing. If you put primitive value types in the list, you have to box and unbox, that is copying them into a reference type and then take them out of it. With generics you have strong typing, but that's not the only

substantial benefit.

In the .NET Framework 2.0, C# and Visual Basic .NET support generics. This means that you can now declare a list of a particular type of object. You can have a List of Integer and a List of ProductInfo, and they will be different types. The compiler can now enforce basic syntax rules and statically verify the type safety of the code you're building.

In C# with generics, you can declare a parametric type as below:

class List<T> 
{
   :
}

In the definition, T is a sort of keyword and represents the type parameter. Within the declaration of the List<T> type, you use T as a real type. For example, you can initialize an internal array of the given type using the code below:

public List(int capacity)
{
   Items = new T[capacity];
}

Put another way, the T keyword works like the formal parameter of a function; like the formal parameter of a function, the type parameter will be determined only when the type is used.

List<ProductInfo> products = new List<ProductInfo>();

With generics, you can model data in the code more easily and conveniently. Building collections, for example, has never been easier. The .NET Framework 2.0 comes with a long list of generics-enabled base classes for which you only have to specify the type parameter. For example, to create a custom collection class you do as follows:

public class ProductInfoCollection : List<ProductInfo> 
{
}

That's it; no other code is required.

Generics in C# and Visual Basic .NET are different from Java generics and C++ templates. If you want to know more, take a look at what Anders Hejlsberg-the father of C#-has to say about it. Read it at http://www.artima.com/intv/generics.html.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.