When and Where to use Generic?


You want to use generic types in a new project or convert nongeneric types in an existing project to their generic equivalent. However, you do not really know why you would want to do this, and you do not know which nongeneric types should be converted to be generic.


You need to consider several things:
  1. Will you type contain or be operating on various on unspecified data types? If so, creating a generic tyep will offer several benefits over creating a nongeneric type. If your type will operate on only a single specific type, then you may not need to create a generic type.
  2. If your type will be operating on value types, so that boxing and unboxing operations will occur, you should consider using generics to prevent the boxing and unboxing operations.
  3. The stronger type checking associated with generic will aid in finding erros sooner, thus shortening your bug-fixing cycle.
  4. Is your conde suffering from "code bloat" with you writing multiple classess to handle diffrent data types on which they operate? It is easier to write the code once and have it just work for each of the data type it operates on.
  5. Generics allow for greater clarity of code. By eliminating code bloat and forcing stronger type checking on your types, your code will be easier to read and understantd.

See Also: See the "Generics Overviews" and "Benefits of Generics" topics in the MSDN documentation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s