I’ve noticed other developers using this technique, but it always confused me. I decided to investigate this morning and came across the following on MSDN (from http://msdn.microsoft.com/en-us/library/d5x73970(v=vs.100).aspx):
public class GenericList<T> where T : Employee
{
...
}
Why would we want to use this method instead of replacing all instances of T with Employee in the class? To me, this seems like a win on maintainability. I can understand restricting to an interface as a means of including classes from different inheritance hierarchies, but inheritance already solves the problem above in a more obvious way, doesn’t it?
Could this be considered a mistake, or would it be a mistake to ‘fix’ code like this?
Because it could be something derived from Employee.
It’s now possible to do…
It’s possible since we know, at compile time, that T = EvilEmployee and that EvilEmployee has an Evilness property. If we were to force the list into a list of Employee that wouldn’t be possible (without using OfType).