Home
Map
Extension MethodUse a custom extension method. Learn extension method syntax with examples.
C#
This page was last reviewed on Nov 29, 2023.
Extension. An extension method has simplified calling syntax. It represents a static method as an instance method. Extension methods can lead to simpler syntax.
This keyword. An extension method uses the this-keyword in its parameter list. It must be located in a static class. We often use special "Extensions" classes.
this
LINQ
An example. Here is a custom extension method. Usually you will want to store your extension method class in a separate source file (like ExtensionMethods.cs) in the project.
Note This file should store a static class with public static extension methods.
public, private
Then In the rest of your source code, you can invoke these extension methods in the same way as instance methods.
Detail An extension method must be static and can be public so you can use it anywhere in your source code.
static
Important You must specify the this-keyword before the appropriate parameter you want the method to be called upon.
using System; public static class ExtensionMethods { public static string UppercaseFirstLetter(this string value) { // Uppercase the first letter in the string. 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(); Console.WriteLine(value); } }
Dot net perls
Second parameter. Here is an extension method that acts on ints. It receives a parameter—the multiplier for the operand. It returns another int so can be used in function call chains.
return
using System; static class Extensions { public static int MultiplyBy(this int value, int multiplier) { // Uses a second parameter after the instance parameter. return value * multiplier; } } class Program { static void Main() { // Ten times 2 is 20. // Twenty times 2 is 40. int result = 10.MultiplyBy(2).MultiplyBy(2); Console.WriteLine(result); } }
40
Notes, keyword. The only difference in the declaration between a regular static method and an extension method is the "this" keyword in the parameter list.
Important If you want the method to receive other parameters, you can include those at the end.
LINQ. There are many extension methods available. These extension methods were written by Microsoft developers and are available in all C# programs targeting recent .NET Framework versions.
Tip On most of the extension methods, you need to add the "using System.Linq" directive to the top of the code.
A discussion. Extension methods can have many arguments. We can use variable "params" arguments with extension methods. Extension methods are static methods—so there is no performance loss.
params
So Extension methods affect the high-level representation of the code, not the low-level implementation.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 29, 2023 (edit link).
Home
Changes
© 2007-2024 Sam Allen.