jump to navigation

Extension methods in C# 3.0 July 8, 2008

Posted by fofo in c# 3.0, LINQ, Visual Studio 2008.
Tags: ,
trackback

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

If you want to learn more about extension methods click here

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: