Skip to content

Switch expression multiple cases: .NET 10, return values, range pattern and guard samples - #2271

Merged
vladimir-pecanac-main merged 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63200-csharp-switch-multiple-cases-return-same-result
Oct 9, 2026
Merged

vladimir-pecanac-main merged 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63200-csharp-switch-multiple-cases-return-same-result

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "Switch Expression With Multiple Cases and Conditions in C#" (https://code-maze.com/csharp-switch-multiple-cases-return-same-result/). Only csharp-basic-topics/SwitchCaseExpressions changes.

Changes

  • Both projects retargeted from net6.0 to net10.0.
  • Package versions, re-queried on NuGet on 2026-10-09 (newest listed stable of each id): Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter 4.5.1, MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0. The folder keeps MSTest; it has no Assert.ThrowsException calls.
  • File-scoped namespace; Main takes no arguments and prints each result.
  • All five methods return string instead of calling Console.WriteLine.
  • SubMultipleCaseResults: 30 now maps to "It is hot today" and a case 35 returns "It is very hot today", matching every other method and the test helper. Its default returns "No weather report", the test helper's string.
  • SubMultipleCaseResultsWithWhen is now SubMultipleCaseWithRangePattern, written with relational and and patterns. The upper range starts at > 150, so 150 is no longer inside both ranges.
  • New SubMultipleCaseWithGuard: a when guard on a relational pattern (even and odd values between 50 and 150).
  • SubMultipleCaseWithNewVersion is now SubMultipleCaseWithOrPattern, expression-bodied.
  • The pleasant temperatures list is a static readonly field instead of a new list per call; resultstring and newresult renamed to result and newResult.
  • Tests assert on return values: the Console.SetOut capture and the Split("-") recovery are gone, names are PascalCase, the range test covers the 150 and 151 boundary, and two tests cover the guard on an even and an odd value.

Local run (SDK 10.0.302, runtime 10.0.10, Windows 10.0.19045): dotnet build -c Release 0 warnings, 0 errors; dotnet test 28 passed, 0 failed; dotnet list package --vulnerable --include-transitive reports no vulnerable packages in either project.

…d 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.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit d795ba0 into CodeMazeBlog:main Oct 9, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant