8.5. Null Collection Elements

Prior to the introduction of generics, the BCL collection classes represented their elements as objects. As such, developers were able to store and retrieve null values from their collections. However, with generics, you now have the luxury of using value types in your collections.

For clients of generic collections, this means you are no longer allowed to supply "null" values when populating your collection. Well, at least not as directly as you're used to. Instead, you'll need to leverage the Nullable<T> type that you saw in Chapter 4. The end result is that you still have a way to use null values in your collections. And you're allowed to get that null behavior without requiring any boxing or unboxing of the elements of your collections.

Get Professional .NET 2.0 Generics 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.