Ziff Davis EnterpriseHeader
Advertisement
Advertisement
Advertisement
Friday, March 23, 2007 10:08 AM/EST

What are Generic Collections?

When Microsoft released .NET 2.0, they introduced a new concept called generics. This concept, in my opinion, has been made out to be far more complex and difficult than it really is. Let's get right to it: A generic collection, contrary to what its name seems to imply, is simply a collection where you specify exactly what type of objects the collection will hold. That's easy enough. Here's a quick example, first in VB.NET, then C#, and finally C++.NET.

VB.NET:

Option Strict On
Imports System.Collections.Generic
Module Module1

    Sub Main()
        Dim d As New Dictionary(Of Int32, String)
        d(1) = "one"
        d(10) = "ten"
        d(50) = "fifty"
        ' The following line won't compile with strict on.
        ' But with strict off, the compiler will cast
        ' 3.1415 to integer 3, and 10 to string "10"
        ' And allow the line
        'd(3.1415) = 10
        Dim item As KeyValuePair(Of Int32, String)
        For Each item In d
            Console.WriteLine(String.Format("d[{0}]={1}", item.Key, item.Value))
        Next

    End Sub

End Module

C#.NET:

using System;
using System.Collections.Generic;

namespace CSharpGenerics
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary d;
            d = new Dictionary();
            d[1] = "one";
            d[10] = "ten";
            d[50] = "fifty";
            // The following line won't compile.
            // d[3.1415] = 10;
            foreach (KeyValuePair item in d) {
                Console.WriteLine(String.Format("d[{0}]={1}", 
                    item.Key, item.Value));
            }
        }
    }
}

C++.NET:

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;

int main(array ^args)
{
	Dictionary d;
	d[1] = "one";
	d[10] = "ten";
	d[50] = "fifty";
	KeyValuePair item;
	for each(item in d) {
		Console::WriteLine(String::Format("d[{0}]={1}", item.Key, item.Value));
	}
    return 0;
}

All three of these programs do the same thing and produce the same output:

They create a Dictionary that holds keys of type Int32, and values of type String. The programs then put a few items in, and then iterate through the collection, printing out the items.

Generic collections allow for stricter compile-time type checking. Except in the case of VB.NET with Option Strict Off, you can't accidentally put the wrong type of object into the collection.

Why the name generic?

When you create a System.Collections.Generics.Dictionary collection, although you're being specific about what type the collection holds, the original collection description itself is generic in the sense it can be re-used for any type. In other words, it's a generic collection used for creating individual but different, strongly-typed collections. Generics can be used in many other places, not just collections. For example, you can describe a function, but not specify the type for a parameter.

TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/10640

Post a Comment

 
 
Advertisement

Syndication

Subscribe: