Using Generic Types
In our discussion about generic types, you learned how a generic type is used repeatedly. Let’s concentrate on the phonebook example a bit more:
var numbers = new Dictionary<string, int> { { "Bart", 911 }, { "John", 119 }};
In the preceding code, we’re making a constructed generic type (sometimes referred to as a closed generic type) by substituting the TKey
type parameter for string
and the TValue
type parameter for int
. As you expect from a great development environment, Visual Studio provides IntelliSense when supplying types for generic type parameters, as shown in Figure 15.9.
Get C# 5.0 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.