posts - 221,  comments - 12451,  trackbacks - 332

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

Post a new comment about this topic
Title  
Name  
Url

Comments   
Protected by Clearscreen.SharpHIPEnter the code you see: