I had the chance to read the overview and specs of Orcas' C# 3.0 and there are lot of interesting things that are put together to support LINQ in the next version of the language. One of the most easily applicable new features are Extension Methods, but what are they used for? Easy, the extend current types with new static methods by simply adding an using directive with the extender class and without adding a line of code to the previous existing classes... let's see an easy example:
namespace Acme.Utilities
{
public static class Extensions
{
public static int ToInt32(this string s) {
return Int32.Parse(s);
}
public static T[] Slice(this T[] source, int index, int count) {
if (index < 0 || count < 0 || source.Length – index < count)
throw new ArgumentException();
T[] result = new T[count];
Array.Copy(source, index, result, 0, count);
return result;
}
}
}
string s = "1234";
int i = s.ToInt32(); // Same as Extensions.ToInt32(s)
int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] a = digits.Slice(4, 3); // Same as Extensions.Slice(digits, 4, 3)
So, this seems quite useful to me right now, and obviously to the framework guys at Redmond. Firstly, the object type could be extended with lot of methods by simply adding the using System to each class, and thus classes will be more clean and modularized, containing only implementation and logic within their domain of action. Obviously, it wouldn't be that useful without C# 2.0 Generics, the cornerstone of all the upcoming features, and without new inference methods in arrays and vars.
Well, extension methods seems a bit like one step forward than implementing interfaces or inheritance... I mean, why would I implement IMyInterface or inherit from a base class to extend it if I can extend my objects by simply adding an using MyNamespace.IMyExtension??
Actually, I have in mind an easy-to-think example of a good scenario to apply method extensions: data access layers and object to relational mappers... Guess the following:
We may have a set of object classes that describe the entities in my scenario, all of them decorated with a set of attributes that declare how they map to my rdbms database (i.e. table name, relations, fields, etc...) Now, guess we have developed a set of classes that contains typical database methods (save, retrieve, update, find, etc...) declared with generic types, and instrumented with the ability to read types attributes to discover what procedures and tables should it call to retrieve data...
In this case, when we have our two dll's compiled (entities and the datamapping framework) it's just a done-puzzle to extend my entities with the new persistence methods and have full access to the database and data without modifying my original entities' code. And going further, this is so reusable that it can be plugged everywhere as far as my original classes contain the required attribute decoration to support the datamapping extension.
It's just a shame that we have to wait that long to see this feature working in our development environments; but you can still test it with the C# 3.0 Community Technology Preview
posted on Monday, October 10, 2005 6:50 PM
Feedback
# C# 3.0, algunas novedades
11/10/2005 4:34 AM |
# C# 3.0, algunas novedades
11/10/2005 8:52 AM |
# re: Extension Methods in C# 3.0
11/18/2007 10:31 PM |
thxx
# re: Extension Methods in C# 3.0
12/8/2007 11:26 PM |
We offer the largest collection of polyphonic ringtones, monophonic ringtones, mobile videos, color wallpapers, color screensavers, real sounds.
# re: Extension Methods in C# 3.0
12/24/2007 8:26 PM |
If you are looking for the replica watch and information about it, you came to the right place.
# re: Extension Methods in C# 3.0
12/26/2007 1:32 PM |
Great thanks for sharing those code.
# Hierarchical Databinding in WPF
5/25/2008 6:24 AM |
I got an email the other day from a friend who was having some trouble getting the WPF TreeView to do
# Hierarchical Databinding in WPF
5/25/2008 10:01 AM |
I got an email the other day from a friend who was having some trouble getting the WPF TreeView to do
# Hierarchical Databinding in WPF
6/15/2008 4:43 PM |
I got an email the other day from a friend who was having some trouble getting the WPF TreeView to do
# re: Extension Methods in C# 3.0
6/22/2008 12:21 AM |
Hi,
Thanks for this good blog text..
We miss your text.
# re: Extension Methods in C# 3.0
6/23/2008 9:52 AM |
Hi,
Thanks for this good blog text..
We miss your tx.
# re: Extension Methods in C# 3.0
6/27/2008 7:14 AM |
Thanks for this good blog text.. oyuntan hugo oyunlari
# re: Extension Methods in C# 3.0
6/28/2008 7:18 AM |
Thanks for this good blog text.. oyuntan zeka oyunlari
# re: Extension Methods in C# 3.0
6/28/2008 10:59 PM |
Thanks for this good blog text.. komik oyunlar
# re: Extension Methods in C# 3.0
6/29/2008 8:52 AM |
Thanks for this good blog text.. bebek oyunlari
# re: Extension Methods in C# 3.0
7/1/2008 1:26 AM |
Thanks for this good blog text.. oyunvar varmisin yokmusun
# re: Extension Methods in C# 3.0
7/2/2008 5:33 AM |
Thanks for this good blog text.. hugo oyunlari
# re: Extension Methods in C# 3.0
7/3/2008 8:48 AM |
Thanks for this good blog text.. kantir
# re: Extension Methods in C# 3.0
7/10/2008 6:39 AM |
Thanks for this good blog text.. en güzel oyunlar
# re: Extension Methods in C# 3.0
7/16/2008 7:35 PM |
thank you..
# re: Upgrading from MCAD to MCPD Web Developer with exam 71-551
7/31/2008 12:28 AM |
very goods, thanks
# re: Extension Methods in C# 3.0
8/13/2008 8:43 PM |
misaki
# re: Extension Methods in C# 3.0
10/9/2008 5:25 AM |
Thank you very much for this information
# re: Extension Methods in C# 3.0
10/10/2008 6:30 AM |
A fantastic story full of insight. I just couldn't stop reading it.
# re: Extension Methods in C# 3.0
10/13/2008 12:02 AM |
Your site is great source of information ....keep it up
# re: Extension Methods in C# 3.0
10/22/2008 12:40 AM |
Carlito Brigante is released from jail after serving five years of a much longer stretch. He vows to go straight and to live life as a fine, upstanding citizen. He even rekindles a romance with his ex-lover. However, Carlito’s associates cannot seem to leave him in peace…his nephew involves him a pool-room gunfight, his lawyer asks him for “favours” which spiral out of control, and even an old friend (now a wheelchair bound paraplegic) tries to get him into trouble with the law. Carlito decides to make a run for Florida and the promise of a new life, but has one final dramatic escape to make from some hoods who believe (wrongly) that he was behind the murder of their boss.
# re: Extension Methods in C# 3.0
10/23/2008 10:19 PM |
True-life account of the military career of Audie Murphy, the most decorated soldier in WWII. Native of Texas, he was placed in charge of his many younger siblings on the death of his mother and decided to join the military at the age of 18 to provide for them. His many acts of bravery and heroism during the US military advance through Italy, France and into Germany earn him increasing rank and responsibility as well as the respect of his comrades in arms. Eventually he receives two dozen of the highest medals the US and France can bestow, culminating in the awarding of the Congressional Medal of Honor.
# re: Extension Methods in C# 3.0
10/26/2008 11:20 AM |
thanks so much Uçak Oyunlari
# re: Extension Methods in C# 3.0
10/28/2008 3:33 AM |
Thanks for this good blog text.. ev arkadasi
# re: Extension Methods in C# 3.0
11/30/2008 12:59 AM |
thanks a lots of..
# re: Extension Methods in C# 3.0
12/20/2008 1:39 PM |
thanks
# re: Extension Methods in C# 3.0
12/20/2008 1:40 PM |
good.
# re: Extension Methods in C# 3.0
12/20/2008 1:41 PM |
that's good.fighting.
1/10/2009 1:48 AM |
thanks baby
# re: Extension Methods in C# 3.0
1/24/2009 8:44 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:44 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:45 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:46 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:46 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:47 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:48 AM |
thanks
# re: Extension Methods in C# 3.0
1/24/2009 8:50 AM |
thanks
# re: Extension Methods in C# 3.0
2/2/2009 11:54 PM |
thanks
# re: Extension Methods in C# 3.0
2/2/2009 11:55 PM |
thanks
# re: Extension Methods in C# 3.0
3/17/2009 9:49 PM |
thanks
# re: Extension Methods in C# 3.0
4/30/2009 8:41 AM |
Thanks.
# re: Extension Methods in C# 3.0
4/30/2009 8:42 AM |
Thanks.
# re: Extension Methods in C# 3.0
4/30/2009 8:42 AM |
Thanks.
# re: Extension Methods in C# 3.0
4/30/2009 8:42 AM |
Thanks.
# re: Extension Methods in C# 3.0
4/30/2009 8:43 AM |
Thanks.
# re: Extension Methods in C# 3.0
5/3/2009 4:19 PM |
This is so reusable that it can be plugged everywhere as far as my original classes contain the required attribute decoration to support the datamapping extension.
# re: Extension Methods in C# 3.0
5/3/2009 5:17 PM |
This is so reusable that it can be plugged everywhere as far as my original classes contain the required attribute decoration to support the datamapping extension.
5/8/2009 10:17 PM |
thanks
5/8/2009 10:36 PM |
THANKS
# re: Extension Methods in C# 3.0
5/26/2009 10:58 PM |
thanks.
# re: Extension Methods in C# 3.0
6/13/2009 4:12 AM |
ty man nice
# re: Extension Methods in C# 3.0
7/6/2009 12:25 AM |
HELLO
# re: Extension Methods in C# 3.0
7/17/2009 5:16 AM |
gelmezsen gel be
# re: Extension Methods in C# 3.0
8/11/2009 7:13 PM |
Thanks, that was getting annoying. Thanks for sharing as well
# re: Extension Methods in C# 3.0
9/8/2009 11:00 PM |
Really interesting post. If anybody has an interesting articles you can share with me.
Thanks,
# re: Extension Methods in C# 3.0
9/27/2009 8:54 AM |
thanks for sharing
# re: Extension Methods in C# 3.0
10/11/2009 7:50 AM |
good share
# re: Extension Methods in C# 3.0
10/11/2009 7:50 AM |
thanks
# re: Extension Methods in C# 3.0
10/11/2009 11:25 PM |
Thanks, good post.
# re: Extension Methods in C# 3.0
11/3/2009 9:23 AM |
nice
# re: Extension Methods in C# 3.0
11/9/2009 3:45 PM |
I recently came accross your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often .
# re: Extension Methods in C# 3.0
11/10/2009 6:39 AM |
thanks you.
# re: Extension Methods in C# 3.0
11/13/2009 7:54 AM |
wow…great collection! Thanks for sharing here.
11/14/2009 2:13 PM |
Thanks Admin wery good :)
# re: Extension Methods in C# 3.0
11/15/2009 1:01 PM |
How are you
# re: Extension Methods in C# 3.0
11/16/2009 3:41 AM |
How are you
# re: Extension Methods in C# 3.0
11/16/2009 5:05 AM |
hii are you
# re: Extension Methods in C# 3.0
11/19/2009 11:03 AM |
how are you baby
# re: Extension Methods in C# 3.0
11/24/2009 1:35 AM |
dizi izle
# re: Extension Methods in C# 3.0
11/24/2009 1:35 AM |
film izle
# re: Extension Methods in C# 3.0
11/24/2009 10:59 AM |
hii are you
# re: Extension Methods in C# 3.0
12/10/2009 2:22 AM |
sinemalar
# re: Extension Methods in C# 3.0
12/22/2009 8:03 AM |
hi how are you all
# re: Extension Methods in C# 3.0
12/23/2009 2:28 AM |
sallaaaa
# re: Extension Methods in C# 3.0
1/1/2010 8:29 PM |
thanks,
# VideoKlip Sesli Chat Sesli Sohbet Webcam SesliChat SesliSohbet
1/16/2010 4:38 PM |
Thank you editors for the topic.Its include a lot of useful informations.i join this blog i couldnt see unnecessary arguments and it makes us happy thnx all moderator n editors.
# VideoKlip Sesli Chat Sesli Sohbet Webcam SesliChat SesliSohbet
1/16/2010 4:39 PM |
Thank you editors for the topic.Its include a lot of useful informations.i join this blog i couldnt see unnecessary arguments and it makes us happy thnx all moderator n editors.
# VideoKlip Sesli Chat Sesli Sohbet Webcam SesliChat SesliSohbet
1/16/2010 4:39 PM |
Thank you editors for the topic.Its include a lot of useful informations.i join this blog i couldnt see unnecessary arguments and it makes us happy thnx all moderator n editors.
# VideoKlip Sesli Chat Sesli Sohbet Webcam SesliChat SesliSohbet
1/16/2010 4:40 PM |
Thank you editors for the topic.Its include a lot of useful informations.i join this blog i couldnt see unnecessary arguments and it makes us happy thnx all moderator n editors.
# re: Extension Methods in C# 3.0
1/17/2010 9:43 PM |
thank you.
# re: Extension Methods in C# 3.0
1/18/2010 8:05 AM |
okey oyna
# re: Extension Methods in C# 3.0
2/5/2010 6:39 AM |
thanksyouu balcony good sites...
# re: Extension Methods in C# 3.0
2/9/2010 4:53 PM |
thanks.
# re: Extension Methods in C# 3.0
2/14/2010 12:05 PM |
Thank you editors for the topic.Its include a lot of useful informations.i join this blog i couldnt see unnecessary arguments and it makes us happy thnx all moderator n editors.
# re: Extension Methods in C# 3.0
2/14/2010 12:06 PM |
thank you.
# re: Extension Methods in C# 3.0
2/20/2010 1:15 AM |
Peace, Love, Unity and Respect.Lets Be a friendship on the net with around the world via internet.Lets be talk about lifestyle like
# re: Extension Methods in C# 3.0
2/20/2010 1:15 AM |
Peace, Love, Unity and Respect.Lets Be a friendship on the net with around the world via internet.Lets be talk about lifestyle like
# re: Extension Methods in C# 3.0
3/1/2010 5:06 PM |
The extension methods made my program a lot better and more web 2.0.
# re: Extension Methods in C# 3.0
3/3/2010 6:53 AM |
thanksss
3/8/2010 3:01 AM |
Thanks very good !.
3/13/2010 12:28 PM |
That what you say help me greatly. Many thanks.
3/13/2010 12:30 PM |
That what you say help me greatly. Many thanks.
3/13/2010 12:31 PM |
That what you say help me greatly. Many thanks.