Skip to content

Convert string to bool: retarget net10.0, MSTest 4.4.0, typed catches, ToBoolOrNull helper - #2211

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65515-csharp-convert-string-to-bool
Sep 21, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65515-csharp-convert-string-to-bool

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the strings-csharp/ConvertStringToBoolean sample alongside a rewrite of the "Convert String to Bool (Boolean) in C#" article.

  • Both projects move from net5.0 (out of support since May 2022) 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 deliberately hold null become string?[], which matches the bool.TryParse(string?, out bool) signature.
  • ParseMethod() now catches ArgumentNullException and FormatException separately instead of bare Exception. Which exception a bad input raises is the point of that section, and the catch-all was hiding it.
  • New ToBoolOrNull() helper maps "1", "0", "yes", "on" and friends, returning null for anything it does not recognise so a caller can tell a false apart from a failure.
  • Packages: Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 4.4.0, coverlet.collector 10.0.1 (versions re-queried on NuGet on 2026-09-15). MSTest 4 removes Assert.ThrowsException<T>, so all fifteen call sites become Assert.ThrowsExactly<T>: every asserted type is the exact type thrown.
  • Tests gain the inputs the article's new comparison table states ("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.

…, 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.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit fac1227 into CodeMazeBlog:main Sep 21, 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