Free Programming Books
Free download ebooks on computer and programming

Free Ebook "Visual C# 2005: A Developer's Notebook" Sample Chapter

Visual C# 2005: A Developer's
Free Download Chapter 1: C# 2.0, grab it now!
Download chapter

Microsoft's C# language has attracted millions to .NET. Now, to make development on this platform quicker and easier, C# 2.0 offers some key changes as part of the upcoming Visual Studio 2005. The C# 2.0 beta is already available, and our unique "all lab, no lecture" guide offers 50 hands-on projects to explore each new feature. Learn what C# 2.0 can do for you now.

< < prev next > >

Create a Type-Safe List Using a Generic Collection

Type safety is the key to creating code that's easy to maintain. A typesafe language (and framework) finds bugs at compile time (reliably) rather than at runtime (usually after you've shipped the product!). The key weakness in C# 1.x was the absence of generics, which enable you to declare a general collection (for example, a stack or a list) that can accept members of any type yet will be type-safe at compile time. In Version 1.x of the framework, nearly all the collections were declared to hold instances of System.Object, and because everything derives from System.Object, these collections could hold any type at all; that is, they were not type-safe.

Suppose, for example, you were creating a list of Employee objects in C# 1.x. To do so, you would use an ArrayList, which holds objects of the System.Object type. Adding new Employees to the collection was not a problem because Employees were derived from System.Object, but when you tried to retrieve an Employee from the ArrayList, all you would get back was an Object reference, which you would then have to cast:

Employee theEmployee = (Employee) myArrayList[1];

An even bigger problem, however, was that there was nothing to stop you from adding a string or some other type to the ArrayList. As long as you never needed to access the string, you would never note the errant type. Suppose, however, that you passed that ArrayList to a method that expected an ArrayList of Employee objects. When that method attempted to cast the String object to the Employee type at runtime, an exception would be thrown.

A final problem with .NET 1.x collections arose when you added value types to the collection. Value types had to be boxed on their way into the collection and explicitly unboxed on their way out.

.NET 2.0 eliminates all these problems with a new library of collections, which you will find in the System.Collections.Generic namespace. A generic collection is simply a collection that allows you to specify its member types when you declare it. Once declared, the compiler will allow only objects of that type to be added to your list. You define generic collections using special syntax; the syntax uses angle brackets to indicate variables that must be defined when an instance of the collection is declared.

There is no need to cast when you retrieve objects from a generic collection, and your code is safer, easier to maintain, and simpler to use than it is with untyped collections such as ArrayList.