Repository navigation
Switch expression multiple cases: .NET 10, return values, range pattern and guard samples - #2271
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/SwitchCaseExpressionschanges.Changes
Maintakes no arguments and prints each result.stringinstead of callingConsole.WriteLine.SubMultipleCaseResults: 30 now maps to "It is hot today" and acase 35returns "It is very hot today", matching every other method and the test helper. Its default returns "No weather report", the test helper's string.SubMultipleCaseResultsWithWhenis nowSubMultipleCaseWithRangePattern, written with relational andandpatterns. The upper range starts at> 150, so 150 is no longer inside both ranges.SubMultipleCaseWithGuard: awhenguard on a relational pattern (even and odd values between 50 and 150).SubMultipleCaseWithNewVersionis nowSubMultipleCaseWithOrPattern, expression-bodied.static readonlyfield instead of a new list per call;resultstringandnewresultrenamed toresultandnewResult.Console.SetOutcapture and theSplit("-")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 Release0 warnings, 0 errors;dotnet test28 passed, 0 failed;dotnet list package --vulnerable --include-transitivereports no vulnerable packages in either project.