An extension method has simplified calling syntax. It represents static methods as instance methods in the syntax of the C# language. This is sometimes more intuitive—and easier to remember—for developers. An extension method uses the 'this' keyword in its parameter list. It must be located in a static class.


Here is a custom extension method defined in a program written in the C# programming language. Generally, you will want to store your extension method class in a separate source file, such as "ExtensionMethods.cs" in your project. The file should store a static class with public static extension methods. In the rest of your source code, you can invoke these extension methods in the same way as you can call instance methods.
This C# article describes extension methods. It provides an example extension method.
Program that uses extension method on string [C#]
using System;
public static class ExtensionMethods
{
public static string UppercaseFirstLetter(this string value)
{
//
// Uppercase the first letter in the string this extension is called on.
//
if (value.Length > 0)
{
char[] array = value.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
return value;
}
}
class Program
{
static void Main()
{
//
// Use the string extension method on this value.
//
string value = "dot net perls";
value = value.UppercaseFirstLetter(); // Called like an instance method.
Console.WriteLine(value);
}
}
Output
Dot net perlsDescription. In the first part of the program text, you can see an extension method declaration in the C# programming language. An extension method must be static and can be public so you can use it anywhere in your source code.
Tip: The extension method is called like an instance method, but is actually a static method. The instance pointer 'this' is received as a parameter. You must specify the 'this' keyword before the appropriate parameter you want the method to be called upon. In the method, you can refer to this parameter by its declared name.

This-keyword in parameter list. The only difference in the declaration between a regular static method and an extension method is the 'this' keyword in the parameter list. If you want the extension method to received other parameters as well, you can place those in the method signature's parameter list after the 'this' parameter.
Calling extension method. You can call an extension method in the same way you call an instance method. In Visual Studio, an extension method in IntelliSense is represented by a different icon that has a downward arrow on it. You can use this icon to differentiate between instance and extension methods before calling them. Also the text "(extension)" is used in IntelliSense.

There are many extension methods available in the .NET Framework currently used. These extension methods were written by Microsoft and are available in all C# programs targeting recent versions of the .NET Framework. On most of the extension methods, you need to add the "using System.Linq" directive to the top of the source text.
LINQ Examples
Extension methods can have many arguments other than the 'this' parameter that is required. You can even use variable 'params' arguments with extension methods, as with any method. Because extension methods are actually static methods, there is no significant performance difference between them and other methods.
Tip: Extension methods are basically an altered-syntax form of instance methods in the C# programming language. They affect the high-level representation of the source text, not the low-level implementation.

We examined extension methods in the C# language. You can add extension methods to any type, even a value type. The original representation of the type does not change. Extension methods affect the syntactic representation of the source. They do not make the execution of the source code substantially different.
Method Tips