Extension Methods in C# 3.0

Introduction

This article explains Extension methods, a new existing feature of C# 3.0. This feature is available in 3.0 and higher versions of C#. An Extension method is very important and very special for programming developers.

An Extension method is special for the class design.  

Why we need Extension Methods

Using an Extension method we can use another class method without changing functionality. But it is very compulsory that an Extension method be defined in a top-level static class.

Generally in our programming we saw that there are two classes, a parent class and a child class in our program. The child class inherits the method of a parent class. it can change the functionality of the parent class method.

But in an Extension method it must not be required to change the functionality of another method without having a sub class. A drawback exists in extension methods, they cannot be used to override an existing method.   

Specification of an Extension Method

An Extension method is a very cool and new feature. We can say this is an instance method. This Extension method is a Static method of a static class. This allows us to add a new method for a predefined method. It increases the functionality of a predefined method.

An Extension method can use another method without inheritance and without changing the source code. In the Extension method we are using the "this" modifier. It is applied as the first parameter and the parameter extends the functionality of this method without changing a parent class method.

Example of  extension Method

  1. public static class Extensionmethod  
  2.     {  
  3.         public static double countTex(this Book b)  
  4.         {  
  5.             return (b.bookPrice * .25);  
  6.         }  
  7.     }  
  8. }  

 

Full code of extension Method

  1. namespace ConsoleApplication1  
  2. {  
  3.       using BOOKS;  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             List<Book> BOOKS = new List<Book>()  
  9.             {  
  10.                 new Book{BookName = "C-sharp", bookPrice = 450},  
  11.                 new Book{BookName = "SQL-server", bookPrice = 750},  
  12.                 new Book{BookName = "VB 2008", bookPrice = 500}  
  13.             };  
  14.             foreach (Book b in BOOKS)  
  15.             {  
  16.                 Console.WriteLine("{0} costs {1} (Tex: {2})", b.BookName, b.bookPrice, b.countTex());  
  17.             }  
  18.         }  
  19.     }   
  20.     public static class Extensionmethod  
  21.     {  
  22.         public static double countTex(this Book b)  
  23.         {  
  24.             return (b.bookPrice * .25);  
  25.         }  
  26.     }  
  27. }  
  28.    
  29. namespace BOOKS  
  30. {  
  31.     public class Book  
  32.     {  
  33.         public string BookName { getset; }  
  34.         public double bookPrice { getset; }  
  35.     }  
  36. }  

 

Output

extension of method output