Extending .NET Framework 3.5

AID: 2105
  • Status: Published

1280 points

  • Byivostoykov
  • TypeTutorial
  • Posted on2009-12-15 at 04:45:05
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");
    }
  }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:

Select allOpen in new window

Asked On
2009-12-15 at 04:45:05ID2105
Tags

C# NET framework method extension

Topic

C# Programming Language

Views
730

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top C# Experts

  1. kaufmed

    566,376

    Sage

    500 points yesterday

    Profile
    Rank: Genius
  2. BuggyCoder

    240,764

    Guru

    10 points yesterday

    Profile
    Rank: Sage
  3. navneethegde

    158,560

    Guru

    0 points yesterday

    Profile
    Rank: Wizard
  4. CodeCruiser

    140,708

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  5. TheLearnedOne

    137,350

    Master

    2,800 points yesterday

    Profile
    Rank: Savant
  6. wdosanjos

    124,511

    Master

    3,500 points yesterday

    Profile
    Rank: Genius
  7. AndyAinscow

    107,357

    Master

    0 points yesterday

    Profile
    Rank: Genius
  8. Dhaest

    98,335

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. Idle_Mind

    91,914

    Master

    0 points yesterday

    Profile
    Rank: Savant
  10. tommyBoy

    90,068

    Master

    0 points yesterday

    Profile
    Rank: Genius
  11. nishantcomp2512

    89,000

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  12. MlandaT

    86,553

    Master

    0 points yesterday

    Profile
    Rank: Genius
  13. Chinmay_Patel

    80,818

    Master

    0 points yesterday

    Profile
    Rank: Genius
  14. ddayx10

    66,538

    Master

    0 points yesterday

    Profile
    Rank: Sage
  15. anarki_jimbel

    66,132

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  16. ambience

    63,764

    Master

    0 points yesterday

    Profile
    Rank: Sage
  17. ukerandi

    62,593

    Master

    1,000 points yesterday

    Profile
    Rank: Guru
  18. apeter

    60,772

    Master

    0 points yesterday

    Profile
    Rank: Sage
  19. JamesBurger

    60,305

    Master

    0 points yesterday

    Profile
    Rank: Sage
  20. sedgwick

    52,750

    Master

    1,600 points yesterday

    Profile
    Rank: Genius
  21. emoreau

    50,917

    Master

    0 points yesterday

    Profile
    Rank: Genius
  22. ged325

    50,311

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  23. anuradhay

    49,977

    2,500 points yesterday

    Profile
    Rank: Guru
  24. techChallenger1

    47,638

    0 points yesterday

    Profile
    Rank: Guru
  25. mas_oz2003

    43,540

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame