Generics with Where Clause

Unless you specify otherwise, T is basically treated like an object. It means that T only have the property and method of object.

Solution:Constrain the Type With Where Clause

1 Constraining Generic Type to Interface or Superclass

2 Constraining Generic Type to Only Reference Types

3 Constraining Generic Type to Only Value Types

4 Constraining Generic Type to Require Default Constructor

Summary

The where clause is an excellent tool to give you more power.

Note: 本文属bnbqian原创。

你可能感兴趣的:(generics)