You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Clarify extension method declaration
Extension methods can only be declared in top-level type. They can't be declared in nested types.
* quick formatting fix
* fix a snippet issue
* one more time
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method.md
+21-30
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,33 @@
1
1
---
2
2
title: "How to implement and call a custom extension method"
3
3
description: Learn how to implement extension methods for any .NET type. Client code can use your methods by adding a reference to a DLL and adding a using directive.
4
-
ms.date: 07/20/2015
4
+
ms.date: 10/02/2024
5
5
helpviewer_keywords:
6
6
- "extension methods [C#], implementing and calling"
7
7
ms.topic: how-to
8
-
ms.assetid: 7dab2a56-cf8e-4a47-a444-fe610a02772a
9
8
---
10
9
# How to implement and call a custom extension method (C# Programming Guide)
11
10
12
-
This topic shows how to implement your own extension methods for any .NET type. Client code can use your extension methods by adding a reference to the DLL that contains them, and adding a [using](../../language-reference/keywords/using-directive.md) directive that specifies the namespace in which the extension methods are defined.
13
-
14
-
## To define and call the extension method
15
-
16
-
1. Define a static [class](./static-classes-and-static-class-members.md) to contain the extension method.
17
-
18
-
The class must be visible to client code. For more information about accessibility rules, see [Access Modifiers](./access-modifiers.md).
19
-
20
-
2. Implement the extension method as a static method with at least the same visibility as the containing class.
21
-
22
-
3. The first parameter of the method specifies the type that the method operates on; it must be preceded with the [this](../../language-reference/keywords/this.md) modifier.
23
-
24
-
4. In the calling code, add a `using` directive to specify the [namespace](../../language-reference/keywords/namespace.md) that contains the extension method class.
25
-
26
-
5. Call the methods as if they were instance methods on the type.
27
-
28
-
Note that the first parameter is not specified by calling code because it represents the type on which the operator is being applied, and the compiler already knows the type of your object. You only have to provide arguments for parameters 2 through `n`.
29
-
30
-
## Example
31
-
32
-
The following example implements an extension method named `WordCount` in the `CustomExtensions.StringExtension` class. The method operates on the <xref:System.String> class, which is specified as the first method parameter. The `CustomExtensions` namespace is imported into the application namespace, and the method is called inside the `Main` method.
Extension methods present no specific security vulnerabilities. They can never be used to impersonate existing methods on a type, because all name collisions are resolved in favor of the instance or static method defined by the type itself. Extension methods cannot access any private data in the extended class.
39
-
11
+
This article shows how to implement your own extension methods for any .NET type. Client code can use your extension methods. Client projects must reference the assembly that contains them. Client projects must add a [using](../../language-reference/keywords/using-directive.md) directive that specifies the namespace in which the extension methods are defined.
12
+
13
+
To define and call the extension method:
14
+
15
+
1. Define a static [class](./static-classes-and-static-class-members.md) to contain the extension method. The class can't be nested inside another type and must be visible to client code. For more information about accessibility rules, see [Access Modifiers](./access-modifiers.md).
16
+
1. Implement the extension method as a static method with at least the same visibility as the containing class.
17
+
1. The first parameter of the method specifies the type that the method operates on; it must be preceded with the [this](../../language-reference/keywords/this.md) modifier.
18
+
1. In the calling code, add a `using` directive to specify the [namespace](../../language-reference/keywords/namespace.md) that contains the extension method class.
19
+
1. Call the methods as instance methods on the type.
20
+
21
+
> [!NOTE]
22
+
>
23
+
> The first parameter is not specified by calling code because it represents the type on which the operator is being applied, and the compiler already knows the type of your object. You only have to provide arguments for parameters 2 through `n`.
24
+
25
+
The following example implements an extension method named `WordCount` in the `CustomExtensions.StringExtension` class. The method operates on the <xref:System.String> class, which is specified as the first method parameter. The `CustomExtensions` namespace is imported into the application namespace, and the method is called inside the `Main` method.
Overload resolution prefers instance or static method defined by the type itself to extension methods. Extension methods can't access any private data in the extended class.
0 commit comments