views:

516

answers:

4

Due to the lack of generic variance in the .NET framework, is it more "correct" to have methods that handle the non-generic versions of the System.Collection interfaces, if the methods are being designed to handle multiple types?

Ideally, once moved to .NET 3.5, the code would modified to change these methods into extension methods.

A: 

Define "Multiple types" do you mean, for example, a List that contains both Cars and Dogs? Or one List that contains Cars and another that contains Dogs? Unless you're doing something "special" I'd say the correct thing to do is to implement the Generic version and not the non-generic version.

WaldenL
A: 

Here is a decent article explaining it.

The only solution that I can see is to follow the advice of the article, and make the method a generic method as well.

But, I am not sure how well it will stand up to being an extension method.

MagicKat
+1  A: 

No, the more "correct" thing to do is to make the methods that handle multiple types generic themselves.

Michael L Perry
Yes precisely. See System.Linq.Enumerable.Select for an example.
David B
+1  A: 

In this case (IEnumerable), implementing the generic version is better, as it derives from IEnumerable (non-Generic), so it becomes a moot point, both are available to you.

casperOne