This post originated from an RSS feed registered with .NET Buzz
by Eric Gunnerson.
Original Post: List or EmployeeList?
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
In current versions of C#, to get a strongly-typed collection, you need to define a separate type for that collection. So if you want to expose a collection of employees, you created an EmployeeCollection class.
With generics, it's now possible to just use List<Employee>, and it's also possible to write a pretty simple version of Employee collection as well:
class EmployeeCollection: List<Employee> {}
So, which one is preferred?
My advice is to prefer List<Employee>, as anybody who looks at such a definition will already know what operations are present on it, while it's not clear what operations an EmployeeCollection might provide.
I would switch to EmployeeCollection when I want to add behavior beyond what List<T> provides (I obviously *have to* switch if I want to add behavior).
This also has the added benefit of not cluttering up the program with types that really don't need to be there.