Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

How to use extension methods with C# 3.0

In this post I would like to talk about a new feature in C# 3.0 that is called extension methods.

Extension methods are used extensively with LINQ, so it is of vital importance that we get a good understanding of extension methods. The reason why extension methods are so important is because this is how LINQ methods like Select,Where methods operate and work.

We can define them as static methods that extend existing classes without having to rely on inheritance or having to change the class’s source code or recompile the class. In the past to achieve the same thing we had to inherit from the selected class and extend it with new methods, if it was not sealed….

In order to better understand extension methods we will create a project in visual studio.

1) Launch Visual studio 2008

2) Create an asp.net web application in C# and call it “extensionmethods”

3) Add a new item to your application, a class file, and name it “myveryusefulextensionmethods.cs

4) Inside your class file you have somehing like this

namespace extension_methods
{
    public class myveryusefulextensionmethods

change the public class myveryusefulextensionmethods with 

static class myveryusefulextensionmethods

basically what we need to understand is that we must have a static class.Inside this static class we can add static methods.

5) We will create a static method inside our static class that just reverses a string. Please note that i use the keyword “this”. Whatever the input parameters are they must be prefixed with the “this” keyword. The code follows

 public static string strReverse(this string theString)
        {
            char[] strArray = theString.ToCharArray();
            Array.Reverse(strArray);
            string strReversed = new string(strArray);
            theString = strReversed;
            return theString;
        }

6) Now we can use this method from our default.aspx page to reverse a string. We will call this extension method from the page load event handler. The code follows

 protected void Page_Load(object sender, EventArgs e)
        {
            string s = “extensionmethods”;
       
            Response.Write(s.strReverse());
        }

When we type Response.Write(s.) we see something like the following picture in our code editor window.

 

Did you spot the new extension method “strReverse” among the other methods? This extension method is highlighted with a blue arrow icon facing down.

7) Run your application by hitting F5. You will get a new web page with “sdohtemnoisnetxe” on it. This is the reverse of “extensionmethods”

We can add more static methods to our static class.

Things to note when using extension methods

  • Use them when you really need them
  • Of course you cannot use extension methods to override existing methods
  • They are called extension methods for a a reason, so no you cannot apply this concept to fields and properties

 

Comments

RichardD said:

I know this is only an example, but your string reversing method won't work as expected if your string contains certain classes of Unicode characters. For more information, have a look at Jon Skeet's blog:

msmvps.com/.../omg-ponies-aka-humanity-epic-fail.aspx

# November 17, 2009 3:23 PM

Anna said:

I learnt a lot from this post...

# September 26, 2010 2:52 PM

ahsanm.m said:

nice and clean presentation easy to understand...thanks

# January 3, 2011 3:18 AM