C#3.0 extension methods and the like for the Gurus

D

Deckarep

I recently got into somewhat of a debate with a fellow co-worker.
First I asked him if he new of a way to add a method to an existing
class like enhancing a class but without using Inheritance. He asked,
"Why on earth would you want to do this? Just inherit." I told him
other dynamic languages can do this such as JavaScript where you can
just add method to say the Array classes prototype chain. I told him I
found this technique often useful and elegant vs. Inheritance.

But, that's how we do this in JavaScript and it doesn't necessarily
mean it's the right way to do things. Now I hear C# is offering
"Extension Methods" where you can add your own methods to the String
class without having to create your own SuperString class and then
using composition (cause String is sealed) to get what you need. I
know the C#3.0 extension methods are probably just compiler magic
making you *think* that you are actually adding methods to these
existing classes but in my opinion it's how you use the technique. I
can still see an added benefit just for mere convention. I mean, most
programmers will happily use native classes like the String class and
expecting them to use SuperString every where in your program to me
seams kinda presumptious especially if you are really only adding a few
methods to it.

So my question as it stands: Is this a good technique? Are there
other reasons to do this technique because I don't really have a leg to
stand on with my co-worker...he sees the idea as pointless and stupid
when you can just inherit. I brought up the JavaScript example...but
then again...JavaScript is just a browser scripting language which
doesn't nearly have the power of C#.
But then again, I kinda feel maybe I'm right seeing Microsoft is adding
this support to C#3.0.

So what do ya'll think?

-Ralph
 
D

Dustin Campbell

So what do ya'll think?

I recently blogged about an interesting side effect of extension methods
here:

http://diditwith.net/PermaLink,guid,0beef993-3417-4ed5-9393-b8b470a42c02.aspx

And at the bottom of this article, I show how extension methods can be used
to "fake" encapsulation on enums by making them appear as if the operations
that act on an enum are actually instance methods of the enum.

http://diditwith.net/PermaLink,guid,5123de5e-3ab6-4d09-b98f-a9a05cef6aad.aspx

Best Regards,
Dustin Campbell
Developer Express Inc
 
J

Jon Skeet [C# MVP]

Deckarep said:
I recently got into somewhat of a debate with a fellow co-worker.
First I asked him if he new of a way to add a method to an existing
class like enhancing a class but without using Inheritance. He asked,
"Why on earth would you want to do this? Just inherit."

That's a bad idea to start with. Inheritance is useful, but it
shouldn't be *overused*. Inheriting to change the behaviour or to add
extra data is one thing, but just to add extra methods is nasty.

See http://msmvps.com/jon.skeet/archive/2006/03/04/inheritancetax.aspx
for more on the reisks of inheritance.
But, that's how we do this in JavaScript and it doesn't necessarily
mean it's the right way to do things. Now I hear C# is offering
"Extension Methods" where you can add your own methods to the String
class without having to create your own SuperString class and then
using composition (cause String is sealed) to get what you need.

Using composition in this kind of situation is a non-starter, because
often you'll be passed a string, or you need to pass a string to
something else. What was originally meant to be a convenience turns
into a nightmare.

What extension methods do is effectively add "utility methods". I often
have classes such as StringUtil which are full of static methods,
almost all of which take a single instance of the appropriate class and
do something with it. Extension methods are *exactly* that, they just
turn a call of myString.ExtraMethod() into
StringUtil.ExtraMethod(myString). It just looks nicer, that's all.

The beauty is that it's done without any of the pitfalls of
inheritance.

My concern around extension methods (as they were originally specified,
anyway) is to do with make it clear what you're doing - last time I
looked, *all* the extension methods within a namespace would be pulled
in at a time; I'd prefer to have it on a class by class basis, eg:

using static Foo.Bar.StringUtil; // adds extension methods to String
 
C

Christof Nordiek

I know the C#3.0 extension methods are probably just compiler magic
making you *think* that you are actually adding methods to these
existing classes but in my opinion it's how you use the technique.

Yes it is sort of compiler magic. Extension methods are simply static
methods where the first parameter is marked with a special modifier. They
can be called as if they were instance methods of the type of that
parameter, but that will be compiled the same way as if it wer called as
static methods.
given following declaration:

void Foo (this int bar, string bogus){...}

and field declaration;:
int i; string s;

following calls ar eqivalent:
i.Foo(s);
Foo(i, s);

PS: I'm not sure about the exact syntax for extension methods. I only wrote
it out of my head.

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top