C# Extension Method

An extension method has simplified calling syntax. It represents static methods as instance methods. An extension method uses the this-keyword in its parameter list. It must be located in a static class.

Static ClassExtension methods in Visual Studio

Example

Method

First, here is a custom extension method defined in a program written in the C# language. Generally, you will want to store your extension method class in a separate source file, such as "ExtensionMethods.cs" in your project.

Note:This file should store a static class with public static extension methods.

Public

Then:In the rest of your source code, you can invoke these extension methods in the same way as instance methods.

C# program that uses extension method on string

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 perls
Program

In the first part of the program, 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 a parameter.

And:You must specify the this-keyword before the appropriate parameter you want the method to be called upon.

This keyword

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 method to receive other parameters, you can include those at the end.

You can call an extension method in the same way you call an instance method. In Visual Studio, an extension method in IntelliSense has a downward arrow on it. This is a visual clue to how methods are represented.

Also:The text "(extension)" is used in IntelliSense. This applies to Visual Studio 2010.

LINQ

LINQ: language integrated query

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.

Tip:On most of the extension methods, you need to add the "using System.Linq" directive to the top of the code.

LINQ

Discussion

Concept

Extension methods can have many arguments. You can even use variable "params" arguments with extension methods, as with any method. Because extension methods are static methods, there is no significant performance difference.

Params

Extension methods are basically an altered-syntax form of instance methods in the C# programming language. They affect the high-level representation of the code, not the low-level implementation.

Summary

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 syntax, not execution.


C#: Method