.NET 2.0 Framework "Generics"

Provided by Jim Ferguson, Senior Systems Architect

The .NET 2.0 framework provides a new feature of the type system called “generics.” With a generic class, instead of specifying the types of parameters or member classes in a class, you can allow the caller, or “user” of your class to specify the types. This allows the user of the class to customize the class for its own needs. When you create a parameter which you wish to make generic, you use a placeholder that differs depending on the language used:

'VB Example

  'Class with generic method

  Public Class TestClass1

    Public Sub GenericMethod(Of T)(ByVal p1 As T)

      MessageBox.Show("The type of T is: " & GetType(T).ToString)

    End Sub

  End Class

 

'Example of calling the method

  Dim c As New TestClass1

  Dim aString As String = "A String"

  Dim anInteger As Integer = 123

 

  c.GenericMethod(aString)

  c.GenericMethod(anInteger)

 

//C# Example

  class TestClass1

  {

    public void GenericMethod<T>(T p1)

    {

      System.Windows.Forms.MessageBox.Show("The type of T is: " + typeof(T).ToString());

    }

  }

 

//Example of calling the method 

  private void button1_Click(object sender, EventArgs e)

  {

    TestClass1 c = new TestClass1();

    string aString = "A String";

    int  anInteger = 123;

   

    c.GenericMethod(aString);

    c.GenericMethod(anInteger);

  }

There are many uses for generics in your own classes, but you can take advantage of them without coding your own generic classes and methods. The .NET 2.0 runtime includes a complete set of generic collection classes which you can use. These include replacements or enhancements for the previous built-in collections such as ArrayList, Dictionary etc.

The standard pre-.net 2.0 ArrayList class can accept objects of any type, but they are always stored and referenced as the generic “Object” type. In order to use the object you need to cast it back into the original data type, and there is no way to prevent objects of different types being stored in the same collection without writing your own wrapper class. Here is an example that uses the standard Arraylist class:

'VB Example

  Dim al As New ArrayList

  ' add the dates

  al.Add(Now.Date)

  al.Add(New Date(2007, 7, 7))

   

  ' iterate the collection, casting back to a date type:

  For Each d As String In al

    ' cast the object into a specific type.

    Dim dt As Date = CDate(d)

    Debug.WriteLine(dt.ToString())

  Next

 

//C# Example

  ArrayList al = new ArrayList();

  // add the dates

  al.Add(DateTime.Now);

  al.Add(new DateTime(2007, 7, 7));

   

  // iterate the collection, casting back to a date type:

  foreach (DateTime d in al)

  {

    // cast the object into a specific type.

    DateTime dt = (DateTime)d;

    System.Diagnostics.Debug.WriteLine(dt.ToString());

  }

While the ArrayList collection is useful, it has several drawbacks. Since it can contain only the Object datatype, you can store any object it it. You may encounter run-time errors if the list contains objects other than the type you expect. Or it will be necessary to test the data type of each object in the collection in order to use it. The collection is not type-safe without writing your own wrapper class, and using Object variables is less efficient than using specific data types.

Here is an example of using the new .NET 2.0 List collection class. When you create an instance of the object you specify the data type of the object it will contain by specifying it in the constructor:

'VB Example

  Dim l As New List(Of Date)

  l.Add(Now.Date)

  l.Add(New Date(2007, 7, 7))

 

  ' The following will receive a run-time error when you attempt to add an invalid type

  'l.Add("A String")

 

  For Each d As Date In l

    ' the object is already strongly typed as a date, so there is no need to cast it

    Debug.WriteLine(d.ToString())

  Next

 

//C# Example

  List<DateTime> l = new List<DateTime>();

  l.Add(DateTime.Now);

  l.Add(new DateTime(2007, 7, 7));

 

  // The following will receive a run-time error when you attempt to add an invalid type

  //l.Add("A String");

 

  foreach (DateTime d in l)

  {

    //the object is already strongly typed as a date, so there is no need to cast it

    System.Diagnostics.Debug.WriteLine(d.ToString());

  }

The topic of the new generic class feature in .NET 2.0 goes far beyond this simple example. Explore this new feature in your own applications in order to make them safer, more efficient, and to reduce code complexity.

Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS