From 2f5573fe1197f34b8a57dd61d748037801ba2b49 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Fri, 9 Oct 2026 00:14:03 +0200 Subject: [PATCH] Switch multiple cases: .NET 10, return values, range pattern and guard samples - 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. --- .../SwitchCaseExpressions/Program.cs | 161 ++++++++------- .../SwitchCaseExpressions.csproj | 2 +- .../SwitchCaseExpressions/Tests/Tests.csproj | 10 +- .../Tests/UnitTestSwitchCase.cs | 186 ++++++++---------- 4 files changed, 173 insertions(+), 186 deletions(-) diff --git a/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/Program.cs b/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/Program.cs index 54feceef37..9027d45b1b 100644 --- a/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/Program.cs +++ b/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/Program.cs @@ -1,95 +1,104 @@ -namespace SwitchCaseExpression +namespace SwitchCaseExpression; + +public static class Program { - public static class Program - { - public static bool In(this T val, params T[] vals) => vals.Contains(val); + private static readonly List pleasantTemperatures = [20, 22, 24]; - static void Main(string[] args) - { - SubMultipleCaseResults(22); - SubMultipleCaseResultsWithWhen(100); - SubMultipleCaseWithExtension(22); - SubMultipleCaseWithListValues(22); - SubMultipleCaseWithNewVersion(22); - } + public static bool In(this T val, params T[] vals) => vals.Contains(val); - public static void SubMultipleCaseResults(int switchTemp) - { - var resultstring = string.Empty; + static void Main() + { + Console.WriteLine(SubMultipleCaseResults(22)); + Console.WriteLine(SubMultipleCaseWithRangePattern(100)); + Console.WriteLine(SubMultipleCaseWithGuard(100)); + Console.WriteLine(SubMultipleCaseWithExtension(22)); + Console.WriteLine(SubMultipleCaseWithListValues(22)); + Console.WriteLine(SubMultipleCaseWithOrPattern(22)); + } - switch (switchTemp) - { - case 20: - case 22: - case 24: - resultstring = "It is a pleasant day"; - break; - case 30: - resultstring = "It is very hot today"; - break; - default: - resultstring = "No weather report today"; - break; - } + public static string SubMultipleCaseResults(int switchTemp) + { + var result = string.Empty; - Console.WriteLine(resultstring); + switch (switchTemp) + { + case 20: + case 22: + case 24: + result = "It is a pleasant day"; + break; + case 30: + result = "It is hot today"; + break; + case 35: + result = "It is very hot today"; + break; + default: + result = "No weather report"; + break; } - public static void SubMultipleCaseResultsWithWhen(int value) + return result; + } + + public static string SubMultipleCaseWithRangePattern(int value) + { + switch (value) { - switch (value) - { - case int n when (n >= 50 && n <= 150): - Console.WriteLine("The value is between 50 and 150"); - break; - case int n when (n >= 150 && n <= 200): - Console.WriteLine("The value is between 150 and 200"); - break; - default: - Console.WriteLine("The number is not within the given range."); - break; - } + case >= 50 and <= 150: + return "The value is between 50 and 150"; + case > 150 and <= 200: + return "The value is between 150 and 200"; + default: + return "The number is not within the given range."; } - public static void SubMultipleCaseWithExtension(int tempValue) - { - var result = tempValue switch - { - var x when x.In(20, 22, 24) => "It is a pleasant day", - 30 => "It is hot today", - 35 => "It is very hot today", - _ => "No weather report.", - }; + } - Console.WriteLine($"{result} - with extension method"); + public static string SubMultipleCaseWithGuard(int value) + { + switch (value) + { + case >= 50 and <= 150 when value % 2 == 0: + return "An even value between 50 and 150"; + case >= 50 and <= 150: + return "An odd value between 50 and 150"; + default: + return "The number is not within the given range."; } + } - public static void SubMultipleCaseWithListValues(int tempValue) + public static string SubMultipleCaseWithExtension(int tempValue) + { + var result = tempValue switch { - var templist = new List { 20, 22, 24 }; - - var newresult = tempValue switch - { - var x when templist.Contains(x) => "It is a pleasant day", - 30 => "It is hot today", - 35 => "It is very hot today", - _ => "No weather report", - }; + var x when x.In(20, 22, 24) => "It is a pleasant day", + 30 => "It is hot today", + 35 => "It is very hot today", + _ => "No weather report.", + }; - Console.WriteLine($"{newresult} - result when using a list"); - } + return result; + } - public static void SubMultipleCaseWithNewVersion(int tempValue) + public static string SubMultipleCaseWithListValues(int tempValue) + { + var newResult = tempValue switch { - var resultText = tempValue switch - { - 20 or 22 or 24 => "It is a pleasant day", - 30 => "It is hot today", - 35 => "It is very hot today", - > 35 => "Heat wave condition", - _ => "No weather report.", - }; + var x when pleasantTemperatures.Contains(x) => "It is a pleasant day", + 30 => "It is hot today", + 35 => "It is very hot today", + _ => "No weather report", + }; - Console.WriteLine($"{resultText} - result is for C# 9.0 syntax"); - } + return newResult; } + + public static string SubMultipleCaseWithOrPattern(int tempValue) => tempValue switch + { + 20 or 22 or 24 => "It is a pleasant day", + 30 => "It is hot today", + 35 => "It is very hot today", + > 35 => "Heat wave condition", + _ => "No weather report." + }; } diff --git a/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/SwitchCaseExpressions.csproj b/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/SwitchCaseExpressions.csproj index 40c60dd4c8..7f1dc2eb2f 100644 --- a/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/SwitchCaseExpressions.csproj +++ b/csharp-basic-topics/SwitchCaseExpressions/SwitchCaseExpressions/SwitchCaseExpressions.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 enable enable diff --git a/csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj b/csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj index e2358a5ef0..6fd7154362 100644 --- a/csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj +++ b/csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj @@ -1,17 +1,17 @@ - net6.0 + net10.0 enable false - - - - + + + + diff --git a/csharp-basic-topics/SwitchCaseExpressions/Tests/UnitTestSwitchCase.cs b/csharp-basic-topics/SwitchCaseExpressions/Tests/UnitTestSwitchCase.cs index e135ba8faa..a5f904fd9f 100644 --- a/csharp-basic-topics/SwitchCaseExpressions/Tests/UnitTestSwitchCase.cs +++ b/csharp-basic-topics/SwitchCaseExpressions/Tests/UnitTestSwitchCase.cs @@ -1,120 +1,98 @@ -using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using SwitchCaseExpression; -using System.IO; -using System.Text; -namespace Tests -{ - [TestClass] - public class UnitTestSwitch - { - private readonly string _pleasantWeather = "It is a pleasant day"; - private readonly string _hotWeather = "It is hot today"; - private readonly string _veryHotWeather = "It is very hot today"; - private readonly string _noWeatherReport = "No weather report"; - - StringWriter stringWrite = new StringWriter(); - - public UnitTestSwitch() - { - Console.SetOut(stringWrite); - } - - public string GetExpectedOutputForTest(int temp) - { - var expectedout = string.Empty; - - switch (temp) - { - case 20: - case 22: - case 24: - expectedout = _pleasantWeather; - break; - case 30: - expectedout = _hotWeather; - break; - case 35: - expectedout = _veryHotWeather; - break; - default: - expectedout = _noWeatherReport; - break; - } - - return expectedout; - } - - [TestMethod] - public void whenMultipleCasesHaveSameResult() - { - var switchTemp = 20; - var expectedout = GetExpectedOutputForTest(switchTemp); - - Program.SubMultipleCaseResults(switchTemp); - - var resultstring = stringWrite.ToString().Trim(); +namespace Tests; - Assert.AreEqual(expectedout, resultstring); - } +[TestClass] +public class UnitTestSwitch +{ + private const string PleasantWeather = "It is a pleasant day"; + private const string HotWeather = "It is hot today"; + private const string VeryHotWeather = "It is very hot today"; + private const string NoWeatherReport = "No weather report"; - [TestMethod] - public void whenMultipleCasesUseWhenKeyword() + private static string GetExpectedOutputForTest(int temp) + { + switch (temp) { - var expoutput = "The value is between 50 and 150"; - - Program.SubMultipleCaseResultsWithWhen(100); - - var resultstring = stringWrite.ToString().Trim(); - - Assert.AreEqual(expoutput, resultstring); - } - - [TestMethod] - public void whenMultipleCaseWithListValues() - { - var tempValue = 22; - var expectedout = GetExpectedOutputForTest(tempValue); - - Program.SubMultipleCaseWithListValues(tempValue); - - var resultstring = stringWrite.ToString(); - var arr = resultstring.Split("-"); - resultstring = arr[0].ToString().Trim(); - - Assert.AreEqual(expectedout, resultstring); + case 20: + case 22: + case 24: + return PleasantWeather; + case 30: + return HotWeather; + case 35: + return VeryHotWeather; + default: + return NoWeatherReport; } + } - [TestMethod] - public void whenSwitchCaseWithEasyFormat() - { - var tempValue = 22; - var resultText = string.Empty; - var expectedoutput = GetExpectedOutputForTest(tempValue); - - Program.SubMultipleCaseWithNewVersion(tempValue); + [TestMethod] + [DataRow(20)] + [DataRow(22)] + [DataRow(24)] + [DataRow(30)] + [DataRow(35)] + [DataRow(99)] + public void WhenMultipleCasesHaveSameResult(int temp) + { + Assert.AreEqual(GetExpectedOutputForTest(temp), Program.SubMultipleCaseResults(temp)); + } - resultText = stringWrite.ToString(); - var arr = resultText.Split("-"); - resultText = arr[0].ToString().Trim(); + [TestMethod] + [DataRow(100, "The value is between 50 and 150")] + [DataRow(150, "The value is between 50 and 150")] + [DataRow(151, "The value is between 150 and 200")] + [DataRow(200, "The value is between 150 and 200")] + [DataRow(201, "The number is not within the given range.")] + public void WhenMultipleCasesUseRangePattern(int value, string expected) + { + Assert.AreEqual(expected, Program.SubMultipleCaseWithRangePattern(value)); + } - Assert.AreEqual(expectedoutput, resultText); - } + [TestMethod] + public void WhenGuardMatchesEvenValue() + { + Assert.AreEqual("An even value between 50 and 150", Program.SubMultipleCaseWithGuard(100)); + } - [TestMethod] - public void whenSwitchCaseWithExtensionMethod() - { - var tempValue = 22; - var expectedout = GetExpectedOutputForTest(tempValue); + [TestMethod] + public void WhenGuardMatchesOddValue() + { + Assert.AreEqual("An odd value between 50 and 150", Program.SubMultipleCaseWithGuard(101)); + } - Program.SubMultipleCaseWithExtension(tempValue); + [TestMethod] + [DataRow(20)] + [DataRow(22)] + [DataRow(24)] + [DataRow(30)] + [DataRow(35)] + public void WhenMultipleCaseWithListValues(int temp) + { + Assert.AreEqual(GetExpectedOutputForTest(temp), Program.SubMultipleCaseWithListValues(temp)); + } - var result = stringWrite.ToString(); - var arr = result.Split("-"); - result = arr[0].ToString().Trim(); + [TestMethod] + [DataRow(20)] + [DataRow(22)] + [DataRow(24)] + [DataRow(30)] + [DataRow(35)] + public void WhenSwitchCaseWithOrPattern(int temp) + { + Assert.AreEqual(GetExpectedOutputForTest(temp), Program.SubMultipleCaseWithOrPattern(temp)); + } - Assert.AreEqual(expectedout, result); - } + [TestMethod] + [DataRow(20)] + [DataRow(22)] + [DataRow(24)] + [DataRow(30)] + [DataRow(35)] + public void WhenSwitchCaseWithExtensionMethod(int temp) + { + Assert.AreEqual(GetExpectedOutputForTest(temp), Program.SubMultipleCaseWithExtension(temp)); } }