Skip to content

Validate a GUID in C#: .NET 10, null-safe validators - #2283

Merged
vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/110071-csharp-how-to-validate-a-guid
Oct 13, 2026
Merged

vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/110071-csharp-how-to-validate-a-guid

Conversation

@vladimir-pecanac-main

@vladimir-pecanac-main vladimir-pecanac-main commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Sample update for the article "Validate a GUID in C# With Guid.TryParse" (https://code-maze.com/csharp-how-to-validate-a-guid/).

Changes, all inside csharp-classes-struct-record/HowToValidateGuid:

  • Both projects move from net8.0 to net10.0.
  • Nullable reference types enabled in both projects (was disable).
  • GuidHelper: all six validators take string? input. ValidateWithGuidParse, ValidateWithGuidParseExact and ValidateWithNewGuid catch ArgumentNullException as well as FormatException (catch (Exception ex) when (ex is FormatException or ArgumentNullException)), because Guid.Parse(null), Guid.ParseExact(null, "D") and new Guid(null) throw ArgumentNullException. ValidateWithRegex returns false for null instead of throwing from Regex.IsMatch.
  • The benchmark class moves from the HowToUseStringPool.Benchmark namespace (copied from another sample) to HowToValidateGuid.Benchmark, in HowToValidateGuidBenchmark.cs and Program.cs.
  • GuidHelperTests: the fixture field is initialised at its declaration, which clears CS8618 now that nullable is on.
  • Eight new tests (18 to 26): a null-input test for each of the six validators, and an X-format test for ValidateWithGuidParseExact and ValidateWithGuidTryParseExact.
  • New GuidHelper.IsUsableGuid (true only for a string that parses as a GUID and is not Guid.Empty), as the article shows it, with four tests: null, a random string and Guid.Empty return false, a real GUID returns true (26 to 30).

Package versions, re-queried on NuGet on 2026-10-11 (latest listed stable):

  • BenchmarkDotNet 0.15.8
  • Microsoft.NET.Test.Sdk 18.10.1
  • MSTest.TestAdapter 4.5.1
  • MSTest.TestFramework 4.5.1
  • coverlet.collector 10.1.0
  • AutoFixture 4.18.1

Local results on SDK 10.0.302 / runtime 10.0.10: dotnet build -c Release 0 warnings 0 errors; dotnet test 30 passed, 0 failed; dotnet list package --vulnerable --include-transitive reports no vulnerable packages in either project.

- Retarget both projects from net8.0 to net10.0
- Enable nullable reference types in both projects
- Take string? in all six GuidHelper validators; the Parse, ParseExact and
  constructor helpers now also catch ArgumentNullException, and the regex
  helper returns false for null instead of throwing
- Move the benchmark class from HowToUseStringPool.Benchmark to
  HowToValidateGuid.Benchmark
- Initialise the test fixture at its declaration (CS8618)
- Add a null-input test for each validator and X-format tests for
  ParseExact and TryParseExact (18 to 26 tests)
- BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1,
  MSTest.TestAdapter and MSTest.TestFramework 4.5.1,
  coverlet.collector 10.1.0, AutoFixture 4.18.1
- IsUsableGuid returns true only for a string that parses as a GUID and
  is not Guid.Empty, as the article shows it
- Four tests: null, a random string and Guid.Empty return false, a real
  GUID returns true (26 to 30 tests)
@vladimir-pecanac-main
vladimir-pecanac-main merged commit c52333c into CodeMazeBlog:main Oct 13, 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