Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Just What's So Great About This Generics Stuff?

Yes, I heard you thinking this question! Look back at the GenericCollection class in Listing 3. Recall that we only made this class concrete (from an application perspective) with the following line in Listing 4:

GenericCollection<Person> genericCollection = new GenericCollection<Person>();

In other words, we can reuse the GenericCollection class in Listing 3 for other collection types. This means that the class in Listing 3 is very generic. If the generic class is carefully designed, it can be reused over and over. What do I mean by "carefully"? Well, I just mean that you should try not to put any application-specific semantics into the generic class. Remember that I said generic code is hard to write! This is an example. How can you keep application semantics out of the generic class? One way is to just put the specific semantics (such as annual leave allocation data) into the subclasses. This technique helps to keep the generic class clean.

In addition to the List type, there are numerous other types in the System.Collections.Generics namespace: Dictionary, Queue, Stack, SynchronizedCollection, and so on. These are also well worth learning. What else can you do with a List type?

  • + Share This
  • 🔖 Save To Your Account