473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Nov 22 '06 #1
3 1631
So what do ya'll think?

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

http://diditwith.net/PermaLink,guid,...470a42c02.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,...05cef6aad.aspx

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 22 '06 #2
Deckarep <de******@gmail.comwrote:
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/...itancetax.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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '06 #3
"Deckarep" <de******@gmail.comschrieb im Newsbeitrag
news:11*********************@m73g2000cwd.googlegro ups.com...
<snip>
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
Nov 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frederik Vanderhaegen | last post by:
Hi, I'm writing a simple soap extension for a webservice I developed (without the use of an extension the webservice works perfect). The extension is registered through the web.config files of...
1
by: gregory.lielens | last post by:
Hello, We are currently writing python bindings to an existing C++ library, and we encountered a problem that some of you may have solved (or that has found unsolvable :( ): A C++ class...
8
by: Steven Nagy | last post by:
Some simple yes/no questions. I tried googling first and couldn't find specific answers. These are related to .Net 3.5 extension methods 1) I found that I would like to add properties as well as...
13
by: llothar | last post by:
On windows everything is '.pyd' but there seems to be two ways to get this on unix? Why and what is the rule?
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
4
by: Rene | last post by:
Hi, I was wondering if anyone could tell me why extension methods must be declared on static classes. I mean, why not allowing them to be declared as static methods in regular instance classes?...
1
by: Simon Woods | last post by:
Hi I have 2 classes which are both instantiated by shared factory methods and both implement a common interface. Public Class Factory Public Shared Function CreateMyClass1() As MyClass1...
3
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.