O'Reilly logo

Microsoft® Visual C#® 2008 Step by Step by John Sharp

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Generics Solution

C# provides generics to remove the need for casting, improve type safety, reduce the amount of boxing required, and make it easier to create generalized classes and methods. Generic classes and methods accept type parameters, which specify the type of objects that they operate on. The .NET Framework class library includes generic versions of many of the collection classes and interfaces in the System.Collections.Generic namespace. The following code example shows how to use the generic Queue class found in this namespace to create a queue of Circle objects:

using System.Collections.Generic; ... Queue<Circle> myQueue = new Queue<Circle>(); Circle myCircle = new Circle(); myQueue.Enqueue(myCircle); ... myCircle = myQueue.Dequeue(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required