how to call an extension method in c# code example

Example 1: define extension methods c#

public static class MyExtensions
{
  //use the this keyword before an argument
  public static int WordCount(this String str)
  {
    return str.Split(new char[] { ' ', '.', '?' },
                     StringSplitOptions.RemoveEmptyEntries).Length;
  }
}

Example 2: HOW TO CALL AN EXTENSION METHOD FOR VIEW C#

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Linq" />
    <add namespace="System.Collections.Generic" />

    <add namespace="MyProject.Extensions" />
  </namespaces>
</pages>