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
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/extension-methods.md
+10-2
Original file line number
Diff line number
Diff line change
@@ -76,9 +76,17 @@ You can use extension methods to extend a class or interface, but not to overrid
76
76
77
77
## Extension members and instance members
78
78
79
-
Overload resolution prefers members declared in a type over extension members. Extension members can't override an existing method. The following example demonstrates the rules that the C# compiler follows in determining whether to bind to an instance member on the type, or to an extension member. The static class `Extensions` contains extension members defined for any type that implements `IMyInterface`. Classes `A`, `B`, and `C` all implement the interface. The `MethodB` extension method is never called because its name and signature exactly match methods already implemented by the classes. When the compiler can't find an instance method with a matching signature, it will bind to a matching extension method if one exists.
79
+
Overload resolution prefers members declared in a type over extension members. Extension members can't override an existing method. The following example demonstrates the rules that the C# compiler follows in determining whether to bind to an instance member on the type, or to an extension member. The static class `Extensions` contains extension members defined for any type that implements `IMyInterface`:
The `MethodB` extension method is never called because its name and signature exactly match methods already implemented by the classes. When the compiler can't find an instance method with a matching signature, it will bind to a matching extension method if one exists.
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/how-to-create-a-new-method-for-an-enumeration.md
+14-3
Original file line number
Diff line number
Diff line change
@@ -12,11 +12,22 @@ ms.topic: how-to
12
12
13
13
You can use extension methods to add functionality specific to a particular enum type. In the following example, the `Grades` enumeration represents the possible letter grades that a student might receive in a class. An extension method named `Passing` is added to the `Grades` type so that each instance of that type now "knows" whether it represents a passing grade or not.
Beginning with C# 14, you can declare *extension members* in an extension block. The new syntax enables you to add *extension properties*. You can also add extension members that appear to be new static methods or properties. You're no longer limited to extensions that appear to be instance methods. The following example shows an extension block that adds an instance extension property for `Passing`, and a static extension property for `MinimumPassingGrade`:
You can learn more about the new extension members in the article on [extension members](./extension-methods.md) and in the language reference article on the ['extension` keyword](../../language-reference/keywords/extension.md).
18
30
19
-
The `Extensions` class also contains a static variable that is updated dynamically and that the return value of the extension method reflects the current value of that variable. The preceding code demonstrates that, behind the scenes, extension methods are invoked directly on the static class in which they're defined.
0 commit comments