<

Extending .NET Framework 3.5

Published on
9,630 Points
3,630 Views
Last Modified:
Approved
Extention Methods in C# 3.0

by Ivo Stoykov

C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance.

These are static methods invoked as instance method. This allows developers to add functionality without necessity to create derived types, re-compiling or other modifications of existing objects. Also to extend sealed objects and avoiding deep digging into object inheritance tree.

Declaration

There are few "must do" in extension method declaration

1. Must be declared into static class
2. Method must be static
3. 1st parameter must be this.

This first parameter in method declaration is the most important one. It specifies witch type the method operates on. The keyword this defines that method is applied on the instance of defined type.

In the sample below we have only one parameter compound by three parts. Part one is the keyword stating that the method WordCount is applicable to String type.

Part two is the type itself -- String in our sample.

Part three is the particular instance of the type the method will be executed -- in the sample this is property name s. In other words the method will be applicable to any String instance.

In the sample we define in Main method a variable of String type named str. In the next row we use standard notation -- type, dot, method, parameters -- str type on the left of the dot becomes out first parameter in WordCount method and, so str becomes s representing String instance passed as this.

This is why extension methods might appear anywhere (same as in the sample or any other file; same or any other project) and will be available all the time if their namespace is explicitly imported into source code with a using directive.


What Extension Method Cannot do

* Extension methods cannot be used to override existing methods
* An extension method with the same name and signature as an instance method will not be called
* The concept of extension methods cannot be applied to fields, properties or events


// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ExtensionMethodTest
{
  static class StrExt
  {
    public static int WordCount(this String s)
    {
      return s.Split(new char[] {'\t', ' ', ',', '.', '?', '!', ';', ':'}, StringSplitOptions.RemoveEmptyEntries).Length;
    }
  }
 
  class Program
  {
    static void Main(string[] args)
    {
      String str = "This string! has; few words! This is, shoerter.";
 
      Console.WriteLine("string " + str + Environment.NewLine + "has " + str.WordCount() + " words");
    }
  }
}

Open in new window

0
Comment
Author:Ivo Stoykov
0 Comments

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Join & Write a Comment

The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month