Click here to Skip to main content
13,194,617 members (50,615 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 2 Jun 2014

Extension Methods in C#

, 2 Jun 2014
Rate this:
Please Sign up or sign in to vote.
This article explains Extension methods.


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.


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.

Using the Code

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class ExtentionMethods
    public static string UpperCaseFirstLetter(this string value)
        if (!string.IsNullOrEmpty(value))
            string[] words = value.Split(' ');
            value = string.Empty;
            foreach (string word in words)
                char[] getWord = word.ToCharArray();
                getWord[0] = char.ToUpper(getWord[0]);
                string getValue = new string(getWord);
                if (string.IsNullOrEmpty(value))
                    value = getValue;
                    value = value + ' ' + getValue;
        return value; 

In the code above, you can see we have a public static class, ExtentionMethods. 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 Extension Method in C# Program

<%@ Page Language="C#" AutoEventWireup="true" 

CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    Name <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" 

            Text="Call Extention Method" OnClick="Button1_Click" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        if (!string.IsNullOrEmpty(txtName.Text))
            string name = txtName.Text;
            txtName.Text = name.UpperCaseFirstLetter();

Look at the code above, we have a textbox, 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 extension 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
LostTheMarbles2-Jun-14 22:04
professionalLostTheMarbles2-Jun-14 22:04 
GeneralMy vote of 1 Pin
anandkiyer2-Jun-14 20:31
memberanandkiyer2-Jun-14 20:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171018.2 | Last Updated 2 Jun 2014
Article Copyright 2014 by Mishra Sourabh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid