Skip to content

Commit d795ba0

Browse files
Switch multiple cases: .NET 10, return values, range pattern and guard samples (#2271)
- Retarget both projects from net6.0 to net10.0; Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0. - File-scoped namespace; Main takes no arguments and prints each result. - All five methods return string instead of writing to the console. - SubMultipleCaseResults: 30 maps to "It is hot today", new case 35, default aligned with the test helper ("No weather report"). - SubMultipleCaseResultsWithWhen renamed to SubMultipleCaseWithRangePattern and rewritten with relational and and patterns; the upper range starts at > 150 so 150 is no longer claimed by both ranges. - New SubMultipleCaseWithGuard showing a when guard on a relational pattern. - SubMultipleCaseWithNewVersion renamed to SubMultipleCaseWithOrPattern, expression-bodied. - The list of pleasant temperatures is a static readonly field instead of a new list per call; resultstring and newresult renamed. - Tests assert on return values: no Console.SetOut capture, no splitting on "-", PascalCase names, range boundary rows, and even and odd guard tests.
1 parent 62ea252 commit d795ba0

4 files changed

Lines changed: 173 additions & 186 deletions

File tree

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,104 @@
1-
namespace SwitchCaseExpression
1+
namespace SwitchCaseExpression;
2+
3+
public static class Program
24
{
3-
public static class Program
4-
{
5-
public static bool In<T>(this T val, params T[] vals) => vals.Contains(val);
5+
private static readonly List<int> pleasantTemperatures = [20, 22, 24];
66

7-
static void Main(string[] args)
8-
{
9-
SubMultipleCaseResults(22);
10-
SubMultipleCaseResultsWithWhen(100);
11-
SubMultipleCaseWithExtension(22);
12-
SubMultipleCaseWithListValues(22);
13-
SubMultipleCaseWithNewVersion(22);
14-
}
7+
public static bool In<T>(this T val, params T[] vals) => vals.Contains(val);
158

16-
public static void SubMultipleCaseResults(int switchTemp)
17-
{
18-
var resultstring = string.Empty;
9+
static void Main()
10+
{
11+
Console.WriteLine(SubMultipleCaseResults(22));
12+
Console.WriteLine(SubMultipleCaseWithRangePattern(100));
13+
Console.WriteLine(SubMultipleCaseWithGuard(100));
14+
Console.WriteLine(SubMultipleCaseWithExtension(22));
15+
Console.WriteLine(SubMultipleCaseWithListValues(22));
16+
Console.WriteLine(SubMultipleCaseWithOrPattern(22));
17+
}
1918

20-
switch (switchTemp)
21-
{
22-
case 20:
23-
case 22:
24-
case 24:
25-
resultstring = "It is a pleasant day";
26-
break;
27-
case 30:
28-
resultstring = "It is very hot today";
29-
break;
30-
default:
31-
resultstring = "No weather report today";
32-
break;
33-
}
19+
public static string SubMultipleCaseResults(int switchTemp)
20+
{
21+
var result = string.Empty;
3422

35-
Console.WriteLine(resultstring);
23+
switch (switchTemp)
24+
{
25+
case 20:
26+
case 22:
27+
case 24:
28+
result = "It is a pleasant day";
29+
break;
30+
case 30:
31+
result = "It is hot today";
32+
break;
33+
case 35:
34+
result = "It is very hot today";
35+
break;
36+
default:
37+
result = "No weather report";
38+
break;
3639
}
3740

38-
public static void SubMultipleCaseResultsWithWhen(int value)
41+
return result;
42+
}
43+
44+
public static string SubMultipleCaseWithRangePattern(int value)
45+
{
46+
switch (value)
3947
{
40-
switch (value)
41-
{
42-
case int n when (n >= 50 && n <= 150):
43-
Console.WriteLine("The value is between 50 and 150");
44-
break;
45-
case int n when (n >= 150 && n <= 200):
46-
Console.WriteLine("The value is between 150 and 200");
47-
break;
48-
default:
49-
Console.WriteLine("The number is not within the given range.");
50-
break;
51-
}
48+
case >= 50 and <= 150:
49+
return "The value is between 50 and 150";
50+
case > 150 and <= 200:
51+
return "The value is between 150 and 200";
52+
default:
53+
return "The number is not within the given range.";
5254
}
53-
public static void SubMultipleCaseWithExtension(int tempValue)
54-
{
55-
var result = tempValue switch
56-
{
57-
var x when x.In(20, 22, 24) => "It is a pleasant day",
58-
30 => "It is hot today",
59-
35 => "It is very hot today",
60-
_ => "No weather report.",
61-
};
55+
}
6256

63-
Console.WriteLine($"{result} - with extension method");
57+
public static string SubMultipleCaseWithGuard(int value)
58+
{
59+
switch (value)
60+
{
61+
case >= 50 and <= 150 when value % 2 == 0:
62+
return "An even value between 50 and 150";
63+
case >= 50 and <= 150:
64+
return "An odd value between 50 and 150";
65+
default:
66+
return "The number is not within the given range.";
6467
}
68+
}
6569

66-
public static void SubMultipleCaseWithListValues(int tempValue)
70+
public static string SubMultipleCaseWithExtension(int tempValue)
71+
{
72+
var result = tempValue switch
6773
{
68-
var templist = new List<int> { 20, 22, 24 };
69-
70-
var newresult = tempValue switch
71-
{
72-
var x when templist.Contains(x) => "It is a pleasant day",
73-
30 => "It is hot today",
74-
35 => "It is very hot today",
75-
_ => "No weather report",
76-
};
74+
var x when x.In(20, 22, 24) => "It is a pleasant day",
75+
30 => "It is hot today",
76+
35 => "It is very hot today",
77+
_ => "No weather report.",
78+
};
7779

78-
Console.WriteLine($"{newresult} - result when using a list");
79-
}
80+
return result;
81+
}
8082

81-
public static void SubMultipleCaseWithNewVersion(int tempValue)
83+
public static string SubMultipleCaseWithListValues(int tempValue)
84+
{
85+
var newResult = tempValue switch
8286
{
83-
var resultText = tempValue switch
84-
{
85-
20 or 22 or 24 => "It is a pleasant day",
86-
30 => "It is hot today",
87-
35 => "It is very hot today",
88-
> 35 => "Heat wave condition",
89-
_ => "No weather report.",
90-
};
87+
var x when pleasantTemperatures.Contains(x) => "It is a pleasant day",
88+
30 => "It is hot today",
89+
35 => "It is very hot today",
90+
_ => "No weather report",
91+
};
9192

92-
Console.WriteLine($"{resultText} - result is for C# 9.0 syntax");
93-
}
93+
return newResult;
9494
}
95+
96+
public static string SubMultipleCaseWithOrPattern(int tempValue) => tempValue switch
97+
{
98+
20 or 22 or 24 => "It is a pleasant day",
99+
30 => "It is hot today",
100+
35 => "It is very hot today",
101+
> 35 => "Heat wave condition",
102+
_ => "No weather report."
103+
};
95104
}

‎csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/SwitchCaseExpressions.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

‎csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66

77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14-
<PackageReference Include="coverlet.collector" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
13+
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
14+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

0 commit comments

Comments
 (0)