Skip to content

Identify if a string is a number: .NET 10, empty-string guard, stricter double check - #2275

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/87852-csharp-identify-if-a-string-is-a-number
Oct 9, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/87852-csharp-identify-if-a-string-is-a-number

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "How to Check If a String Is a Number in C#" (https://code-maze.com/csharp-identify-if-a-string-is-a-number/). Only strings-csharp/IdentifyIfStringIsANumber changes.

Changes

  • All three projects retargeted from net7.0 to net10.0.
  • UsingCharIsDigit, UsingCharIsDigitWithForeach and UsingCharIsBetween09 returned true for an empty string, because All() over an empty sequence is true and a foreach over an empty string never runs its body. Each now returns false for null or empty input. Every theory gains an [InlineData("", false)] row, so the suite goes from 42 to 49 tests.
  • DoubleTryParse switches from NumberStyles.Any to NumberStyles.Float | NumberStyles.AllowThousands with CultureInfo.InvariantCulture kept. NumberStyles.Any accepted "(5)" (accounting notation) as a number; the article never explains that style, and no test covered the line.
  • Program.cs: the "Using Char.IdDigit:" label is fixed and the program now prints all seven approaches instead of four.
  • BenchmarkRunner: a [Params] input "a12345678" (fails on its first character) runs next to "123456789", so the early-exit advantage of the loops is measured.

Packages (re-queried on NuGet 2026-10-09)

  • Microsoft.NET.Test.Sdk 17.5.0 to 18.10.1
  • xunit 2.4.2 to 2.9.3
  • xunit.runner.visualstudio 2.4.5 to 4.0.0
  • coverlet.collector 3.2.0 to 10.1.0
  • BenchmarkDotNet 0.13.5 to 0.15.8

Verification on .NET SDK 10.0.302, runtime 10.0.10, Windows 10.0.19045

  • dotnet build -c Release: 0 warnings, 0 errors
  • dotnet test -c Release: 49 passed, 0 failed
  • dotnet list package --vulnerable --include-transitive: no vulnerable packages in any project
  • BenchmarkDotNet run: 14 benchmarks, completed

…er double check

- Retarget all three projects from net7.0 to net10.0.
- Bump Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0,
  coverlet.collector 10.1.0, BenchmarkDotNet 0.15.8.
- UsingCharIsDigit, UsingCharIsDigitWithForeach and UsingCharIsBetween09 now return false
  for an empty string (All() over an empty sequence is true and an empty foreach never runs).
  Add an empty-string row to all seven theories (42 tests to 49).
- DoubleTryParse uses NumberStyles.Float | NumberStyles.AllowThousands instead of
  NumberStyles.Any, so "(5)" is no longer accepted; InvariantCulture stays.
- Program.cs: fix the "Char.IdDigit" label and demonstrate all seven approaches.
- Benchmark: add a second input, "a12345678", that fails on its first character.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 65d35b6 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