The problem is that, without generics, you don't have a choice. And,
Tell me one reason why to use collections containing objects.
This should only be used very very rarely and with lots ofconsideration
done.
You must be pretty lucky if you never have to deal with any code written
before 2.0 or any code written for the framework which can't use
generics(which, honestly, is still pretty common).
Generics, currently, are *not* CLS compliant and therefore cannot be used in
CLS compliant apis(like the vast majority of the framework). As a result,
there will be plenty of times where generic collections will be out of the
question. That is a reason unto itself.
Also, existing framework code cannot change. So in any situation where you
have to deal with lists of object now, you still will.
Even most of the new apis debuting in Whidbey use object instead of
generics...why you ask? Because Generics aren't CLS compliant and those
apis, like the entire rest of the framework, have to be.
Generics aren't going to obsolete System.Collections, atleast not yet, just
complement it.
Strictly speaking, there is no such thing like "strongly typed". If a have
a
conventional list, it can contain object and all of its descendants.
If I have a list<IFoo> it can contain IFoo and *all* of its implementing
classes.
Strongly typed is simply a limiter, I would wonder about someones abilitiy
if they assumed an array of IFoo is going to contain all FooBar's. All it
promises is that it contains IFoo instances and its silly, IMHO, for a coder
to write code that assumes otherwise. As I siad, if you want to do that,
your a fool if you do that with foreach.
Maybe we have to invent a new feature will really allows "very strongly
typed" collections, so that exactly one certain type is allowed, but this
cannot be caught by the compiler.
That would be pretty useless, I think. Who needs that feature?
And, you don't need a feature anyway, you should be able to do Type
comparisons between the two in a custom collection
if (o.GetType() == typeof(MyFoo)
to ensure the type matches exactly.
As I said the compiler won't complain it an implicit cast from baseclass
to
descentant will take place which *may* be valid, or may not
Nor should it. Otherwise you'd *never* be able to use foreach over a
collection of object(or any existing implementation of IEnumerator) without
getting a warning. When you force warnings into what is one of the most
commonly used constructs in the language(and one that will be for a long
time for most people) you are probably doing something wrong. This goes
along way towards invalidating the *entire* non-generic foreach pattern and
pushing people back to for(foreach object in collection with internal casts
is just stupid as a general pattern). I think the problem here is the way
you are using foreach, not foreach itself.
I may support this around C# v5; but not now, not by a long shot.
Beyond that, any C# dev worth his salt knows that foreach effectivly
contains an explicit cast. You define the type and you should know ahead of
time that a cast is going to occur. IEnumerator works on object, just like
every other generic interface. Its something you have to know.
Suit yourself.
If I have a list<string> and I write foreach (ListViewItem in list) this
invokes a compiler error,
sure, but thats another matter as you already noticed.
Yes, but atleast it makes sense.