You want to improve the syntax for calling common methods in your C# program, so that function names are shorter and easier to type. Extension methods provide a way to easily represent static methods as instance methods in the syntax of the C# language, which can be more intuitive and recallable for developers. Here we look at extension methods.
Example
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.
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 perls
Description. 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.
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.
What’s the Specification of an Extension Method?
An extension method is a special kind of
static
method that allows you to add new methods to existing types without creating derived types. The extension methods are called as if they were instance methods from the extended type, For example: x
is an object from int
class and we called it as an instance method in int
class.How to Create my Extension Method?
Simply, you create your own
static
method in your class and put this keyword in front of the first parameter in this method (the type that will be extended).General Tips in Extension Methods Usage
This section is optional reading section for understanding the core idea of extension methods:
- This keyword has to be the first parameter in the extension method parameter list.
- Extension methods are used extensively in C# 3.0 and further version specially for LINQ extensions in C#, for example:
- It is important to note that extension methods can't access the
private
methods in the extended type. - If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
- If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called.
No comments:
Post a Comment