Generic Constraints

So far, you’ve learned how to declare a generic type and how to use it. Although the latter operation will be your main point of contact with the world of generics, you’ll sometimes want to write your own generic type. However, up to this point, we haven’t been able to do much with a generic type parameter inside the type definition. Let’s take a look from an abstract point of view first. Consider the following piece of code:

image

The main question is what we can do with the input received as a parameter on Bar. For one thing, we can take the object and store it somewhere (for example, in a private field). That’s precisely what ...

Get C# 4.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.