views:

127

answers:

5

Example:

System.Web.Security.MembershipCollection implements IEnumerable and not IEnumberable<T>. Why doesn't it implement the latter, when it seems that it would be better (e.g. use LINQ)?

Or, is it not necessarily better?

+2  A: 

These classes often date from before there were generics in .net many such classes have generic equievelents but not all. There are also workarounds. e.g. with linq any non generic collections can be made linqy and generic using the .Cast() and .OfType() extension methods.

Ben Robinson
+1 for using "linqy"
Chris Dwyer
Linqy is a good word,buti prefer the one coined by a coleague after adding a bunch of UpdatePanels to his asp.net page. He declared he had just "ajaxified" his page ;-)
Ben Robinson
+10  A: 

You can use LINQ with any IEnumerable by using the Cast<T>() function or the OfType<T>() function. If you're confident that the IEnumerable only contains objects of a particular type, then Cast<T>() will be slightly faster.

For example,

ArrayList foo = new ArrayList();

foo.Add("bar");
foo.Add("baz");

var bar = foo.Cast<string>().Select(s => s.ToUpper());

There are many existing classes (like ArrayList) that existed before the advent of generics in .NET, so they are still non-generic.

Adam Robinson
How much of a performance hit will this be?
Chris Dwyer
@Chris: Very little with `Cast`, and only very slight with `OfType` (since type checking is done twice).
Adam Robinson
+2  A: 

Keep in mind that many of the BCL types we're using are from the time before generics, which arrived in 2.0. However, you can still use LINQ with many of them, you simply have to do a little extra work.

ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(2);

var query = from item in arrayList.OfType<int>() select item;
Anthony Pegram
This will *filter* the elements in the ArrayList to only those that are of type int.
Mauricio Scheffer
+12  A: 

History matters. Generics didn't always exist, so you may encounter classes and APIs that were designed before the advent of generics.

Also, target audience matters. Some features are targeting a developer audience that may have problems understanding generics:

Tradeoff: APIs using some advanced features of Generics may be too difficult to use for some developers. The concept of Generics is not widely understood, in some cases the syntax may pose problems, and as any large new feature, Generics may pose a significant learning curve for some entry-level developers.

Yes, the quote is from 2004, but some, if not most of the .Net API you use today came out in 2005, so the quote is actually very relevant.

Remus Rusanu
+1 In previous discussions w/ MS personnel, they're very reticent to make breaking changes to public APIs (or even additive changes to interfaces - as this would break existing interface implementors). if you're a pure consumer, it sound's like a pain in the arse, but once you're in their position (producing APIs for others' consumption), you quickly begin to empathize and follow similar practices as it makes your own life easier and facilitates more rapid adoption of newer, better supported versions.
Nathan Ernst
A: 

Did anybody tried this with ENUM...

Maverick