Another article is posted from
Professional .Net 2.0 Generics by Tod Golding. Here's the intro:
Using Generic Methods
So much attention gets paid to the new generic classes that have been introduced with .NET generics, I often find developers overlooking what can be achieved through the use of generic methods. In fact, in many cases, you may discover one of the first opportunities to leverage generics is by introducing some generic methods into your non-generic types. This brings the flavor and power of generics to a class without requiring the class itself to be parameterized.
The Basics
To illustrate the fundamental value of generic methods, let's start with the simplest of examples. Suppose you have a Max() function that accepts two double values, compares them, and returns the greater of the two values. This function might appear as follows:
[
VB code]
Public Function Max(ByVal val1 As Double, ByVal val2 As Double)
As Double
Return IIf(val2 < val1, val1, val2)
End Function
[C# code]
public double Max(double val1, double val2) {
return (val2 < val1) ? val1 : val2;
}
...
That's the beginning. You can read the
rest of the article online here.
Jim Minatel
Senior Acquisitions Editor
Wiley Technology Publishing
WROX Press
Blog:
http://wroxblog.typepad.com/
Jim's Book of the week:
No book this week - Donate to the Red Cross!