Extension Methods in C#

Extension methods are static methods, that are always implemented in a static class. They don’t need a class object, they can be directly called. In C# we already have some built-in extension methods like Union, Where and Zip. These extension methods need a System.Linq namespace declaration.
 
extension methods 
 
In C#,  Extension methods can be identified by this logo. Extension methods can be defined in C# in a separate .cs file.
 
The extension method is called like an instance method, but is actually a static method. The instance pointer "this" is a parameter and you must specify the "this" keyword before the appropriate parameter you want the method to be called upon.
 
The difference between a regular static method and an extension method is that, in a static method we first provide the class name and then we call the method. In the extension method the "this" keyword is in the parameter list. You can directly call this by a variable.
 
So let’s create an Extension method.
  1. Open Visual Studio
  2. "File" ">>" New >> "Web Site..."
  3. Select "ASP.NET Empty Web Site" , then click "Ok"
  4. Add a new WebForm
  5. Add a new Class
ExtentionMethods.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. /// <summary>  
  7. /// Summary description for ExtentionMethods  
  8. /// </summary>  
  9. public static class ExtentionMethods  
  10. {  
  11.     public static string UpperCaseFirstLetter(this string value)  
  12.     {  
  13.         if (!string.IsNullOrEmpty(value))  
  14.         {  
  15.             string[] words = value.Split(' ');  
  16.             value = string.Empty;  
  17.             foreach (string word in words)  
  18.             {  
  19.                 char[] getWord = word.ToCharArray();  
  20.                 getWord[0] = char.ToUpper(getWord[0]);  
  21.                 string getValue = new string(getWord);  
  22.                 if (string.IsNullOrEmpty(value))  
  23.                 {  
  24.                     value = getValue;  
  25.                 }  
  26.                 else  
  27.                 {  
  28.                     value = value + ' ' + getValue;  
  29.                 }   
  30.             }   
  31.         }  
  32.         return value;   
  33.     }  
  34. }  
In the code above you can see we have a public static class, ExtentionMetods. In this class there is a method "public static string UpperCaseFirstLetter(this string value)" in which the value is obtained and then this function converts the first letter to uppercase.
 
Call Extention method in C# programme

Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     Name <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  11.     </div>  
  12.         <div>  
  13.             <asp:Button ID="Button1" runat="server" Text="Call Extention Method" OnClick="Button1_Click" />  
  14.         </div>  
  15.     </form>  
  16. </body>  
  17. </html>  
Default.aspx.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7. public partial class _Default : System.Web.UI.Page    
  8. {    
  9.     protected void Page_Load(object sender, EventArgs e)    
  10.     {    
  11.     }    
  12.     protected void Button1_Click(object sender, EventArgs e)    
  13.     {    
  14.         if (!string.IsNullOrEmpty(txtName.Text))    
  15.         {    
  16.             string name = txtName.Text;    
  17.             txtName.Text = name.UpperCaseFirstLetter();    
  18.         }    
  19.     }    
  20. }  
Look at the code above, we have a text box, txtName, and a Button. When the user enters text into the TextBox and clicks on the button, it executes the click event code and we have a name variable, that is a string type.
 
Look at this line:
 
txtName.Text = name.UpperCaseFirstLetter();
 
Here we call our UpperCaseFirstLetter extention method in the name variable. You remember our method parameter was defined like this, the string value. That’s why when you use a name variable it comes directly into the Intellisense.
 
C# extension method 
 
That’s it; press F5 and run your code.