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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.