Understanding Generics

Generics are not a new feature; however, they are such an important aspect of LINQ to SQL that it is worth using a little space to review this feature.

Note

To use generics, you need to import the System.Collections.Generic namespace.

I most often use generics by taking advantage of generic collections. For example, if you want to represent a list of strings, you can declare a list of strings like this (in C#):

List<string> stuffToBuy = new List<string>();stuffToBuy.Add("socks");stuffToBuy.Add("beer");stuffToBuy.Add("cigars");

Here’s how you would declare the list of strings in VB.NET:

Dim stuffToBuy As New List(Of String)stuffToBuy.Add("socks")stuffToBuy.Add("beer")stuffToBuy.Add("cigars")

And, by taking advantage of ...

Get ASP.NET 4 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.