Skip to content

Commit 504145d

Browse files
committed
update samples on enums
1 parent ac49017 commit 504145d

File tree

7 files changed

+141
-68
lines changed

7 files changed

+141
-68
lines changed

docs/csharp/programming-guide/classes-and-structs/extension-methods.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ You can use extension methods to extend a class or interface, but not to overrid
7676

7777
## Extension members and instance members
7878

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`:
8080

81-
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="MegaDemoBreakThisUp":::
81+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="InterfaceAndExtensions":::
82+
83+
Classes `A`, `B`, and `C` all implement the interface:
84+
85+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="Classes":::
86+
87+
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.
88+
89+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="CallExtensionMethods":::
8290

8391
## Common usage patterns
8492

docs/csharp/programming-guide/classes-and-structs/how-to-create-a-new-method-for-an-enumeration.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ ms.topic: how-to
1212

1313
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.
1414

15-
<<TODO: New extension members style too>>
15+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="ExtendEnumType":::
1616

17-
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="EnumMethods":::
17+
You can call the extension method as though it was declared on the `enum` type:
18+
19+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMethods.cs" id="ExampleExtendEnum":::
20+
21+
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`:
22+
23+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMembers.cs" id="EnumExtensionMembers":::
24+
25+
You call these new extension properties as though they are declared on the extended type:
26+
27+
:::code language="csharp" source="./snippets/ExtensionMembers/CustomExtensionMembers.cs" id="EnumExtensionMembers":::
28+
29+
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).
1830

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.
2031

2132
## See also
2233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using CustomExtensionMethods;
2+
3+
namespace CustomExtensionMembers;
4+
5+
public enum Grades
6+
{
7+
F = 0,
8+
D = 1,
9+
C = 2,
10+
B = 3,
11+
A = 4
12+
};
13+
14+
// <EnumExtensionMembers>
15+
public static class EnumExtensions
16+
{
17+
private static Grades minimumPassingGrade = Grades.D;
18+
19+
extension(Grades grade)
20+
{
21+
public static Grades MinimumPassingGrade
22+
{
23+
get => minimumPassingGrade;
24+
set => minimumPassingGrade = value;
25+
}
26+
27+
public bool Passing => grade >= minimumPassingGrade;
28+
}
29+
}
30+
// </EnumExtensionMembers>
31+
32+
public static class ExtensionMemberUsage
33+
{
34+
public static void Examples()
35+
{
36+
// <ExampleExtendEnum>
37+
Grades g1 = Grades.D;
38+
Grades g2 = Grades.F;
39+
Console.WriteLine($"First {(g1.Passing ? "is" : "is not")} a passing grade.");
40+
Console.WriteLine($"Second {(g2.Passing ? "is" : "is not")} a passing grade.");
41+
42+
Grades.MinimumPassingGrade = Grades.C;
43+
Console.WriteLine($"\r\nRaising the bar. Passing grade is now {Grades.MinimumPassingGrade}!\r\n");
44+
Console.WriteLine($"First {(g1.Passing ? "is" : "is not")} a passing grade.");
45+
Console.WriteLine($"Second {(g2.Passing ? "is" : "is not")} a passing grade.");
46+
/* Output:
47+
First is a passing grade.
48+
Second is not a passing grade.
49+
50+
Raising the bar!
51+
52+
First is not a passing grade.
53+
Second is not a passing grade.
54+
*/
55+
// </ExampleExtendEnum>
56+
}
57+
}

docs/csharp/programming-guide/classes-and-structs/snippets/ExtensionMembers/CustomExtensionMethods.cs

+54-60
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ public static int WordCount(this string str) =>
88
}
99
//</ClassicExtensionMethod>
1010

11-
//<MegaDemoBreakThisUp>
12-
// Define an interface named IMyInterface.
11+
//<InterfaceAndExtensions>
1312
public interface IMyInterface
1413
{
15-
// Any class that implements IMyInterface must define a method
16-
// that matches the following signature.
1714
void MethodB();
1815
}
1916

@@ -23,28 +20,21 @@ public interface IMyInterface
2320
// class that implements IMyInterface.
2421
public static class Extension
2522
{
26-
public static void MethodA(this IMyInterface myInterface, int i)
27-
{
28-
Console.WriteLine
29-
("Extension.MethodA(this IMyInterface myInterface, int i)");
30-
}
23+
public static void MethodA(this IMyInterface myInterface, int i) =>
24+
Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");
3125

32-
public static void MethodA(this IMyInterface myInterface, string s)
33-
{
34-
Console.WriteLine
35-
("Extension.MethodA(this IMyInterface myInterface, string s)");
36-
}
26+
public static void MethodA(this IMyInterface myInterface, string s) =>
27+
Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");
3728

3829
// This method is never called in ExtensionMethodsDemo1, because each
3930
// of the three classes A, B, and C implements a method named MethodB
4031
// that has a matching signature.
41-
public static void MethodB(this IMyInterface myInterface)
42-
{
43-
Console.WriteLine
44-
("Extension.MethodB(this IMyInterface myInterface)");
45-
}
32+
public static void MethodB(this IMyInterface myInterface) =>
33+
Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");
4634
}
35+
//<InterfaceAndExtensions>
4736

37+
// <Classes>
4838
// Define three classes that implement IMyInterface, and then use them to test
4939
// the extension methods.
5040
class A : IMyInterface
@@ -66,11 +56,31 @@ public void MethodA(object obj)
6656
Console.WriteLine("C.MethodA(object obj)");
6757
}
6858
}
59+
// </Classes>
60+
61+
//<ExtendEnumType>
62+
public enum Grades
63+
{
64+
F = 0,
65+
D = 1,
66+
C = 2,
67+
B = 3,
68+
A = 4
69+
};
70+
71+
// Define an extension method in a non-nested static class.
72+
public static class Extensions
73+
{
74+
public static bool Passing(this Grades grade, Grades minPassing = Grades.D) =>
75+
grade >= minPassing;
76+
}
77+
//</ExtendEnumType>
6978

70-
class ExtMethodDemo
79+
public static class ExtensionMethodUsage
7180
{
72-
static void Main(string[] args)
81+
public static void Examples()
7382
{
83+
// <CallExtensionMethods>
7484
// Declare an instance of class A, class B, and class C.
7585
A a = new A();
7686
B b = new B();
@@ -104,36 +114,20 @@ static void Main(string[] args)
104114
c.MethodA(1); // C.MethodA(object)
105115
c.MethodA("hello"); // C.MethodA(object)
106116
c.MethodB(); // C.MethodB()
107-
}
108-
}
109-
/* Output:
110-
Extension.MethodA(this IMyInterface myInterface, int i)
111-
Extension.MethodA(this IMyInterface myInterface, string s)
112-
A.MethodB()
113-
B.MethodA(int i)
114-
B.MethodB()
115-
Extension.MethodA(this IMyInterface myInterface, string s)
116-
C.MethodA(object obj)
117-
C.MethodA(object obj)
118-
C.MethodB()
119-
*/
120-
//</MegaDemoBreakThisUp>
121-
122-
//<EnumMethods>
123-
// Define an extension method in a non-nested static class.
124-
public static class Extensions
125-
{
126-
public static bool Passing(this Grades grade, Grades minPassing = Grades.D)
127-
{
128-
return grade >= minPassing;
129-
}
130-
}
131-
132-
public enum Grades { F = 0, D = 1, C = 2, B = 3, A = 4 };
133-
class Program
134-
{
135-
static void Main(string[] args)
136-
{
117+
/* Output:
118+
Extension.MethodA(this IMyInterface myInterface, int i)
119+
Extension.MethodA(this IMyInterface myInterface, string s)
120+
A.MethodB()
121+
B.MethodA(int i)
122+
B.MethodB()
123+
Extension.MethodA(this IMyInterface myInterface, string s)
124+
C.MethodA(object obj)
125+
C.MethodA(object obj)
126+
C.MethodB()
127+
*/
128+
// </CallExtensionMethods>
129+
130+
// <ExampleExtendEnum>
137131
Grades g1 = Grades.D;
138132
Grades g2 = Grades.F;
139133
Console.WriteLine($"First {(g1.Passing() ? "is" : "is not")} a passing grade.");
@@ -142,15 +136,15 @@ static void Main(string[] args)
142136
Console.WriteLine("\r\nRaising the bar!\r\n");
143137
Console.WriteLine($"First {(g1.Passing(Grades.C) ? "is" : "is not")} a passing grade.");
144138
Console.WriteLine($"Second {(g2.Passing(Grades.C) ? "is" : "is not")} a passing grade.");
145-
}
146-
/* Output:
147-
First is a passing grade.
148-
Second is not a passing grade.
139+
/* Output:
140+
First is a passing grade.
141+
Second is not a passing grade.
149142
150-
Raising the bar!
143+
Raising the bar!
151144
152-
First is not a passing grade.
153-
Second is not a passing grade.
154-
*/
145+
First is not a passing grade.
146+
Second is not a passing grade.
147+
*/
148+
// </ExampleExtendEnum>
149+
}
155150
}
156-
//</EnumMethods>

docs/csharp/programming-guide/classes-and-structs/snippets/ExtensionMembers/ExtensionMembers.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<LangVersion>preview</LangVersion>
89
</PropertyGroup>
910

1011
</Project>

docs/csharp/programming-guide/classes-and-structs/snippets/ExtensionMembers/ExtensionMethods.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExtensionMembers;
1+
 namespace ExtensionMethods;
22
public static class ExtensionMethods
33
{
44
public static void Examples()
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
// See https://aka.ms/new-console-template for more information
2-
Console.WriteLine("Hello, World!");
1+

2+
ExtensionMethods.ExtensionMethods.Examples();
3+
CustomExtensionMethods.ExtensionMethodUsage.Examples();
4+
CustomExtensionMembers.ExtensionMemberUsage.Examples();

0 commit comments

Comments
 (0)