C#3.0 extension methods and the like for the Gurus



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

.



Relevant Pages

  • Re: Terms for method types?
    ... this is just basic prototype ... on this stage - about the exact inheritance mechanics, ... var myInstance = new MyObject; ... First off, I was talking about javascript, not java. ...
    (comp.lang.javascript)
  • Re: Terms for method types?
    ... on this stage - about the exact inheritance mechanics, ... could freely use the regular English words like class, prototype, ... First off, I was talking about javascript, not java. ... In this case your understanding of the nature of the closure is not ...
    (comp.lang.javascript)
  • Re: Koyunbaba.... We Have Answers!!! uh? Whatd you say?
    ... >> Koyunbaba because of the flavor of the C sharp minor tuning,and the ... The suggested technique will work. ... > strings and pick up a third string in measure 3 on page 19. ... > of resonance in that context it's confusing. ...
    (rec.music.classical.guitar)
  • Re: time for a Benson Picking thread?
    ... The entire technique for people who didn't evolve an unusual picking ... wrist and thumb. ... The whole point of the rotated pick is to strike the string at an angle ... the thumb and index fingers are pointed more toward the neck than in ...
    (rec.music.makers.guitar.jazz)
  • Re: How come Ada isnt more popular?
    ... container varies, the element does not. ... The user should be able to describe string type in language terms ... but there are no generics instantiated. ... Implementation inheritance without values ...
    (comp.lang.ada)