2005-07-25
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 3 )
Some things are good in their own time. Bell bottoms were deemed to be good in the 1970s, but today, after a short revival, they are considered goofy. In contrast, blue jeans have always been deemed to be good, whether you choose Wranglers or Levi's. The same thing is true of code idioms: some idioms are always good, and some are good in their own time.
For example, templates are deemed good, but for VB.NET programmers their time hadn't come until now. Called generics in .NET, Microsoft is introducing generics into languages like C# and VB.NET. (Microsoft could have introduced generics into VB earlier, but the audience probably just wasn't ready.)
Unlike bell-bottoms, generics have always been useful, because they help eliminate the need to write the same algorithm over and over when the only thing that changes is data type — think Stack algorithm — and they help eliminate ugly conditional type checking and coercion. Now, C# and VB.NET support generics.
Historically, generics and templates have been perceived as very hard to use, but they aren't, not really. In this article, I demonstrate how to define generic methods and tell you about their implementation in .NET 2.0.
The Problems Generics Solve
.NET has a common type system. This means all objects have, at their absolute root, a class named object, even simple types like int/Integer. In a technical sense, this means that you could write an abstract data type, Stack (although one already exists), and store objects in it. Since Stack already exists, Listing 1 offers a simple example that illustrates a problem with stacks based on a common subtype.
Listing 1: Problems with common sub-types and data structures.
Imports System.Collections
Module Module1
Sub Main()
Dim s As Stack = New Stack
s.Push(New Gun)
s.Push(New Camera)
While (s.Count > 0)
Dim o As Object = s.Pop
o.Aim()
o.Shoot()
End While
Console.ReadLine()
End Sub
End Module
Public Class Gun
Public Sub Aim()
End Sub
Public Sub Shoot()
Console.WriteLine("You're dead")
End Sub
End Class
Public Class Camera
Public Sub Aim()
End Sub
Public Sub Shoot()
Console.WriteLine("You're a supermodel")
End Sub
End Class
From the example in Listing 1, you see that we can put any type of object in the basic Stack class. The result is that, without a lot of type checking and type coercion, we can't be sure that some unexpected type hasn't found its way in the stack, resulting in an unanticipated runtime error, or worse, supermodels with holes in them.
One solution is to write a type-specific Stack for each kind of object we want to store using a stack. Generics eliminate the need to write type-specific stacks (or anything else), and eliminate the need to for type checking and type coercion. Well, slightly more precisely, Generics eliminate the need to us to write type-specific algorithms, which in turn also eliminates the need to write type-checking and type-coercing code.
Using a Generic Stack
The problem is easily mitigated by using generics. In our supermodel-homicide example, we can use the System.Collections.Generic.Stack class, which would prevent us from shooting the supermodel with the Gun, because Gun wouldn't be permitted in a stack of cameras (see Listing 2).
Listing 2: A revision of listing 1 that uses a Generic stack ensuring type homogeneity.
Imports System.Collections.Generic.
Module Module1
Sub Main()
'Dim s As Stack = New Stack
Dim s As Stack(Of Camera) = New Stack(Of Camera)
's.Push(New Gun)
s.Push(New Camera)
While (s.Count > 0)
Dim o As Object = s.Pop
o.Aim()
o.Shoot()
End While
Console.ReadLine()
End Sub
End Module
Public Class Gun
Public Sub Aim()
End Sub
Public Sub Shoot()
Console.WriteLine("You're dead")
End Sub
End Class
Public Class Camera
Public Sub Aim()
End Sub
Public Sub Shoot()
Console.WriteLine("You're a supermodel")
End Sub
End Class
In the revised example, trying to put Gun in the Stack(Of Camera) would report a compile-time error, which effectively means we wouldn't be shooting supermodels with guns, even by accident.
![]() |
|


