What constraints, if any, to apply to the type parameters (See Constraints on Type Parameters).Ī good rule is to apply the maximum constraints possible that will still let you handle the types you must handle. However, too much generalization can create code that is difficult for other developers to read or understand.
Which types to generalize into type parameters.Īs a rule, the more types you can parameterize, the more flexible and reusable your code becomes. When creating your own generic classes, important considerations include the following: Typically, you create generic classes by starting with an existing concrete class, and changing types into type parameters one at a time until you reach the optimal balance of generalization and usability. For more information about using these classes, see Generic Collections in. Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored.įor most scenarios that require collection classes, the recommended approach is to use the ones provided in the. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.
Generic classes encapsulate operations that are not specific to a particular data type.