Axel,
Again, please refer to my reply to Jon. They are unnecessary. It would be
possible keep the pretty syntax without using them.
No, it would not be possible without some major breaking changes to the
framework. LINQ is effectively grafted *on* rather than *in* to
IEnumerable, and this was only possible via extension methods. LINQ can be
cumbersome enough as it is without having to make the syntax totally
unreadable by means of passing original collections into LINQ static methods
and have them return filtered IEnumerables. Generics simply wouldn't cut it
as a solution.
However, as I wrote Peter, it's not the usage. The usage within Linq is
fine. But implementing own Extension Methods, that's a slap >in the face.
It's the concept. And I still believe that Linq would have had the same
syntax without invention of Extension Methods.
How could it possibly maintain its syntax without Extension Methods? The
only alternative is to actually embed the methods onto the original
IEnumerable classes rather than providing what is essentially a LINQ
exo-skeleton for the IEnumerables. Doing so would've meant big changes to
the classes in question. I'd rather know my 2.0 code is still going to run,
thankyou very much.
As for "implementing own Extension Methods", presumably you mean to say that
you feel MS has slapped us in the face by giving us the option of writing
methods to extend classes we don't have the source code to, and which could
potentially even be sealed? Why is that a bad thing?
I've spent some time rewriting a few utility "helper" classes here and there
to be simple modules full of Extensions, and it's improved the syntax in
large areas of my app considerably as well as making coding a lot more
intuitive. Saying goodbye to the infamous "SqlHelper" class was a happy day
indeed as far as I'm concerned.
I perfectly agree with you. I will use them (in Linq) and I'll like Linq
because of its usage. But I won't get to like Extension Methods >as they
contradict OOD paradigm of keeping an obvious member function within the
class itself.
Wuh oh, paradigm violation, someone call the partyvan... :-D
And, sure, things can be very much abused with anything. But creating
custom Extension Methods would be such abuse. They >shouldn't have been
invented. A generic would have done the trick. Or the invention of multiple
inheritance. Both OOD conform.
If you genuinely believe Generics alone could have been used to implement
LINQ with identical syntax, I suspect you still don't fully understand what
Extension methods do and are for. As for multiple inheritance, I would
strongly question as to whether that does truly conform to OOD in this day
and age. Multi-inheritance creates a dizzying array of new problems to be
solved, and again doesn't create the same elegant syntax of extension
methods.
Perhaps you could explain how you think either Generics or Multi-Inheritance
could be used to mimic the current syntax of LINQ without breaking
compatibility?
Having a LINQ<T> class that surrounds existing generic collection classes
would look awful:
Dim myList as Linq(Of Generic.List(Of MyType))
....and now your IEnumerable is actually of type Linq rather than List, which
will impact your code if you're passing that as a parameter anywhere else.
Multiple inheritance? Presumably you'd then have (for example) a List<T>
class in the framework that inherited from its regular base class,
implemented IEnumerable, *and* inherited from a Linq base class? But then
it's no longer strictly the same type as the v2.0 List<T> class, so you
break masses of code.
Remember that .NET 3.5 is essentially v2.0 with bolt ons, one of which is
LINQ. It still has to be backwards compatible. I can see how extension
methods were born of necessity for the purposes of injecting LINQ into the
framework in a non-breaking manner, and some might call it a kludge, but
it's one I welcome with open arms.
There's a lot of weird, badly implemented and overly cryptic crap in .NET
3.5 --- things I think we could have done without, things that are heading
in the wrong direction, things that are just down right not finished yet and
shouldn't have been released. LINQ and all its many framework-enhancing
children however is not on that list. In my opinion, anyway ;-)
-Alex