3. Working with Generics

Some articles and papers might lead you to believe that generics are useful only in the context of collections. That’s not true. There are many other ways to use generics. You can use them to create interfaces, event handlers, common algorithms, and more.

Many other discussions compare C# generics to C++ templates, usually to advocate one as being better than the other. Comparing C# generics to C++ templates is useful to help you understand the syntax, but that’s where the comparison should end. Certain idioms are more natural to C++ templates, and others are more natural to C# generics. But, as you’ll see in Item 19 a bit later in this chapter, trying to decide which is “better” will only hurt your understanding of both ...

Get Effective C#: 50 Specific Ways to Improve Your C#, Third Edition 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.