Thursday, September 27, 2007

C# 3.0 Extension Methods

Posted also at: http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/09/27/c-3-0-extension-methods.aspx

How many times have you written wrapper methods for objects that in reality you wished were part of the object itself?For example, we have the following method which returns the first and last char of a given string in upper case:

   1:  namespace MaorDavidsBlogExamples
   2:  {
   3:      public static class Extensions
   4:      {
   5:          public static string UpperFirstAndLast(string str)
   6:          {
   7:              string ret = "{0}{1}{2}";
   8:              ret = String.Format(ret,
   9:                  str.Substring(0, 1).ToUpper(),
  10:                  str.Substring(1, str.Length - 2),
  11:                  str.Substring(str.Length-1).ToUpper());
  12:              return ret;
  13:          }
  14:      }
  15:  }


 
We called this method:

   1:  static void Main(string[] args)
   2:  {
   3:      string str = "maordavid";
   4:      Console.WriteLine(Extensions.UpperFirstAndLast(str));
   5:      Console.ReadLine();
   6:  }

...when really, what you want is something more readable like this:


   1:  Console.WriteLine(str.UpperFirstAndLast());

That is exactly what Extension methods let you do.


Extension methods new feature of C# 3.0. They are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods. Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.


 


Now we want to add this new UpperFirstAndLast() method to the existing string type. Some of us can think that we can create our own class that inherits System.String and add that method there, but it's impossible - System.String is a seald class. What can we do? use Extension Methods!


Declaring Extension methods


Extension methods are declared by specifying the keyword this as a modifier on the first parameter of the methods. Extension methods can only be declared in static classes.


Let's rewrite the UpperFirstAndLast:


   1:  namespace MaorDavidsBlogExtensionExamples
   2:  {
   3:      public static class Extensions
   4:      {
   5:          public static string UpperFirstAndLast(this string str)
   6:          {
   7:              string ret = "{0}{1}{2}";
   8:              ret = String.Format(ret,
   9:                  str.Substring(0, 1).ToUpper(),
  10:                  str.Substring(1, str.Length - 2),
  11:                  str.Substring(str.Length-1).ToUpper());
  12:              return ret;
  13:          }
  14:      }
  15:  }

 


The static method above has a "this" keyword before the first parameter argument of type string.  This tells the compiler that this particular Extension Method should be added to objects of type "string". 


To add this specific Extension Method implementation to string instances within my code, I simply use a standard "using" statement to import the namespace containing the extension method implementation. (Line 6 below)

The compiler will then correctly resolve the UpperFirstAndLast() method on any string.


   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  using MaorDavidsBlogExtensionExamples;
   7:   
   8:  namespace MaorDavidsBlogExamples
   9:  {
  10:      class Program
  11:      {
  12:          static void Main(string[] args)
  13:          {
  14:              string str = "maordavid";
  15:              Console.WriteLine(str.UpperFirstAndLast());
  16:              Console.ReadLine();
  17:          }
  18:      }
  19:  }

 


The intellisense will help you to find the extension method: new icon indicates an "extension method.


 





 


Another posts about C# 3.0:


No comments: