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: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access