What Is New in C# 3.0 - Part 3 (Extension Methods)

   Part 3 of the "What Is New in C# 3.0" series deals with extension methods. However before we proceed lets make a little trip through the history of methods as we know it just for the sake of our younger readers (namely Hristinka).

   Shortly after the Big Bang what scientists call subroutines began forming in the assembly language. Subroutines were a sequence of instructions that could be named. The name was replaced by the address of the first instruction by the assembler in the generated binary code. They could accept arguments by looking up the values residing on some addresses or more often registers (which the programmer had to set before calling the subroutine) and return values by setting the values on some addresses or registers which the programmer had to look up after calling the subroutine. Later the assemblers evolved to the point where they could handle subroutines accepting arguments and returning values. No one can be sure that subroutines worked like this or if they even existed. This was so long ago that it is only hypothesis and scientists could not prove it by examining the remaining pieces of assembly code.

   Billions of years later when the dinosaurs roamed the earth the greatest of them called Dennis Ritchie created the C programming language. This highly advanced language had this highly advanced feature called "function". Functions at that time were defined in global scope and could access only global variables but they also supported argument lists and could have a return value. Everything was type-safe. The arguments could be pointers (passed by reference) or variables (passed by values). By passing a pointer to a function the programmer could even pass another function as an argument. Functions were defined like this:

int dbl(int arg)
   return arg*2;

and called like this:

int a = dbl(2);

   As you can see things have not changed much and methods nowadays look very similar to dinosaur style functions.

   Many years later when the Neanderthals ruled the Earth the most clever of them called Bjarne Stroustrup designed the C++ programming language painting pictures with coal on the cave walls. He thought up this ingenious little thing called classes that were just like C's structs but they could be inherited, could have private members and also could have attached functions. These functions are called methods and are very popular today. The methods had access to global scope and also the scope of the class. Some of the methods were called static and could be called without an instance of the class. These methods were absolutely the same like functions except you needed the class name to call them outside the class and also they had access to the static members inside the class (i.e. they were in the scope of the class instead of global scope). The instance methods on the other hand could be called only from an instance. Instance methods are also the same like functions but they have their first (or last or middle it is all the same) argument is hidden and is the instance that called them itself. Thus when calling them you actually write their first argument in front of the function call. This type of functions proved to be very helpful because it helped programmers move the most used functions for a class packed with the class itself which made them easier to find.

   In present days Anders (Hallowed be his name) thought something like:
"Well it is very cool that the creator of a class can define a list of instance methods to be packed with the class, but it would be even cooler if the client programmer could add methods to that package without inheriting the class."

   Well, to be honest that is probably very simplified version of what he thought. It may be even completely wrong but we need some way to express what he was thinking. Because he is a god no human mind can understand what his metaphysical mind actually thinks. If instance methods are just functions with first argument the instance that calls them why cannot we just attach static methods with first argument of type X to the list of methods for an instance of type X. So the following syntax was born for defining what is now known as "extension methods":

public static bool IsNullOrEmpty(this string arg)
   return string.IsNullOrEmpty(arg);

because the first argument is preceded by the keyword this, the method can be called from any instance of the string class like normal instance methods:


The method shows up in intellisense just like instance method except that it has different icon to indicate it is an extension method. Extension methods can be defined only in static classes. The extension method will be visible wherever the static class is visible. It will not flood intellisense in places where it is not needed. Common example for good use of extension method is adding an IsValidEmail() method to the string class like this:

public static bool IsValidEmail(this string arg)
   Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
   return regex.IsMatch(arg);

   Why should we use extension methods instead of just calling the static method from the static class? For the very same reasons programmers write instance methods instead of creating static class and write static methods. It makes the code more readable and it makes possible finding the method in intellisense. This is good in many cases but it is especially evident when you need to call several extension methods consecutively. Lets say we have some methods that look like this:

public static string AppendA(this string arg)
   return arg+"A";

public static string AppendB(this string arg)
   return arg+"B";

With no extension methods if we would like to use this methods to append A and B to a string we would have to write something like this.

string result = Appender.AppendB(Appender.AppendA("mring"));

and with extension methods:

string result = "mring".AppendA().AppendB();

  Imagine what would it be if we needed to call 5-6 methods. In fact that is exactly what LINQ uses extension methods for (I mean calling several extension methods one after another not appending the alphabet to strings).

  Just to make sure you do not think extension methods can have only one parameter here is an example for extension method with additional parameters that returns void:

public static void PrintNTimes(this string arg, int n)
   for(int i = 0; i < n; i++)

and the call:


Cya and stay tuned for part 4.

P.S. Yes I know that C was not the first language to have functions and I know C++ was not the first language to have classes but I used them as a well known examples.
Tags:   english programming 
Last edited by:   Stilgar
on   20:57 22.03.2008
Posted by:   Stilgar
04:02 10.02.2008


First Previous 1 Next Last 

Posted by   ... (Unregistered)   on   13:37 10.02.2008

Do you want enlarge your penis up to 4 inches? Use extension methods!

Posted by   Stilgar   on   15:51 10.02.2008

The new spam systems are quite impressive. Now they use context sensitive spam.

First Previous 1 Next Last 

Post as:

Post a comment: