Skip to content

Commit 4aea8bb

Browse files
psaxtonBillWagner
authored andcommitted
Update discard-pattern2.cs (Issue #8727) (#936)
* Update discard-pattern2.cs (Issue #8727) Changed implementation to actually use the switch and discard language features being demonstrated. * Update discard-pattern2.cs Fixed whitespace differences * Update discard-pattern2.cs * Change switch order Updated switch case order to more closely follow the original code.
1 parent 992a21d commit 4aea8bb

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

snippets/csharp/programming-guide/discards/discard-pattern2.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ public static void Main()
1515

1616
private static void ProvidesFormatInfo(object obj)
1717
{
18-
if (obj is IFormatProvider fmt)
19-
Console.WriteLine($"{fmt} object");
20-
else if (obj is null) {
21-
Console.Write("A null object reference: ");
22-
Console.WriteLine("Its use could result in a NullReferenceException");
18+
switch (obj)
19+
{
20+
case IFormatProvider fmt:
21+
Console.WriteLine($"{fmt} object");
22+
break;
23+
case null:
24+
Console.Write("A null object reference: ");
25+
Console.WriteLine("Its use could result in a NullReferenceException");
26+
break;
27+
case object _:
28+
Console.WriteLine("Some object type without format information");
29+
break;
2330
}
24-
else
25-
Console.WriteLine($"Some object type without format information");
2631
}
2732
}
2833
// The example displays the following output:

0 commit comments

Comments
 (0)