Convert string to bool: retarget net10.0, MSTest 4.4.0, typed catches, ToBoolOrNull helper - #2211
Merged
vladimir-pecanac-main merged 2 commits intoSep 21, 2026
Conversation
…, ToBoolOrNull helper - Both projects move from net5.0 to net10.0, with ImplicitUsings and Nullable enabled. - Program.cs becomes top-level statements calling a static StringToBoolean class in a file-scoped namespace; the arrays that hold null become string?[]. - ParseMethod() catches ArgumentNullException and FormatException separately, so the example shows which exception each bad input raises instead of hiding both. - New ToBoolOrNull() helper maps "1", "0", "yes", "on" and friends, returning null for anything it does not recognise. - Packages: Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter/TestFramework 4.4.0, coverlet.collector 10.0.1. MSTest 4 removes Assert.ThrowsException<T>, so all fifteen call sites become Assert.ThrowsExactly<T>. - Tests gain the inputs the article's comparison table needs and cover the new helper.
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.
Updates the
strings-csharp/ConvertStringToBooleansample alongside a rewrite of the "Convert String to Bool (Boolean) in C#" article.net5.0(out of support since May 2022) tonet10.0, withImplicitUsingsandNullableenabled.Program.csbecomes top-level statements calling a staticStringToBooleanclass in a file-scoped namespace. The arrays that deliberately holdnullbecomestring?[], which matches thebool.TryParse(string?, out bool)signature.ParseMethod()now catchesArgumentNullExceptionandFormatExceptionseparately instead of bareException. Which exception a bad input raises is the point of that section, and the catch-all was hiding it.ToBoolOrNull()helper maps"1","0","yes","on"and friends, returningnullfor anything it does not recognise so a caller can tell afalseapart from a failure.Microsoft.NET.Test.Sdk18.10.1,MSTest.TestAdapterandMSTest.TestFramework4.4.0,coverlet.collector10.0.1 (versions re-queried on NuGet on 2026-09-15). MSTest 4 removesAssert.ThrowsException<T>, so all fifteen call sites becomeAssert.ThrowsExactly<T>: every asserted type is the exact type thrown."TRUE", bare"false"/"False"," true "on both throwing methods, bare"yes"and"y") and three tests for the new helper.Build: 0 warnings, 0 errors. Tests: 9 of 9 passing on SDK 10.0.302 / runtime 10.0.10.