Constraints

When a type parameter is defined, the eventual type argument is unknown. The type parameter could become anything. Only one thing is guaranteed. The type argument will inherit System.Object, which limits the functionality of the type argument to the System.Object interface.

In the following code, ZClass is generic and has a single type parameter, which is T. The code expects that T will be a collection. ZClass.MethodA will enumerate that collection and display each item. Collections should implement the IEnumerable interface. This is also necessary for the foreach loop. Unfortunately, the C# compiler spots a problem. Regardless of how the code expects to use T, T is an implied System.Object, which does not implement the IEnumerable ...

Get Programming Microsoft® Visual C#® 2008: The Language 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.