Casting

You might need to a cast an instance of a type argument. Because type arguments are implicitly System.Object types, the instance can always be cast to that type. In addition, the instance also can be cast to a derivation constraint because the type argument is related to the constraint, and therefore a safe cast is assured. Finally, an instance of a type argument can be cast to any interface even if the interface is not listed as an interface constraint. Because there is no restriction on casting to interfaces, it is not type-safe. For that reason, care should be taken to ensure that you appropriately cast to an implemented interface.

In the following code, ZClass is a generic type. It has a single type parameter (T) that has three constraints: ...

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.