Skip to content

Parse and TryParse: retarget net10.0, MSTest 4.3.3, cover the .NET 7/8 overloads - #2169

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63785-parse-tryparse
Open

Parse and TryParse: retarget net10.0, MSTest 4.3.3, cover the .NET 7/8 overloads#2169
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63785-parse-tryparse

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets the Parse and TryParse sample to .NET 10 and covers the two overloads the article's new section introduces.

Changes

  1. net6.0 -> net10.0 on both ParseAndTryParseInCSharp.csproj and Tests/Tests.csproj.
  2. Test stack to the current line: Microsoft.NET.Test.Sdk 16.11.0 -> 18.9.0, MSTest.TestAdapter / MSTest.TestFramework 2.2.7 -> 4.3.3, coverlet.collector 3.1.0 -> 10.0.1.
  3. Assert.ThrowsException<T> -> Assert.Throws<T>, twelve call sites across the eight negative tests. MSTest 4.x removes ThrowsException<T> outright (CS0117), so this is required by the bump rather than optional tidying.
  4. Two new tests for overloads the sample never exercised: int.TryParse(string?, IFormatProvider?, out int) (the IParsable<T> shape) and int.TryParse(ReadOnlySpan<byte>, out int) (the IUtf8SpanParsable<T> shape).
  5. int.Parse(null) emitted CS8625 under <Nullable>enable</Nullable>. Now int.Parse(null!). A (string?)null cast does not clear it, because the parameter is annotated non-nullable.
  6. int.TryParse(null, out int nullNum) no longer compiles at all on net10.0: CS0121, ambiguous between int.TryParse(string?, out int) and the .NET 8 int.TryParse(ReadOnlySpan<byte>, out int). Now (string?)null, which compiles and still returns false.
  7. ReadOnlySpan<char> value = null; needed no change. Checked explicitly against current C# conversion rules on net10.0: it still compiles with no warning.
  8. Program.cs to a file-scoped namespace. The console project is an empty Main; everything the article shows lives in the tests.

Verification

  • dotnet build -c Release: 0 warnings, 0 errors
  • dotnet test -c Release: 20/20 passing, SDK 10.0.302

The sample carries no platform-bound or environment-bound test, so nothing here needs Live naming and the workflow is untouched.

…8 overloads

Retarget both projects from net6.0 to net10.0 and bring the test stack up to
the current line: Microsoft.NET.Test.Sdk 18.9.0, MSTest 4.3.3, coverlet 10.0.1.

MSTest 4.x removes Assert.ThrowsException<T>, so the twelve call sites across
the eight negative tests move to Assert.Throws<T>.

Add two tests for overloads the sample did not exercise: the IFormatProvider
overload introduced with IParsable<T>, and the UTF-8 ReadOnlySpan<byte>
overload introduced with IUtf8SpanParsable<T>.

Two null call sites needed a fix under <Nullable>enable</Nullable> on net10.0:
int.Parse(null) emitted CS8625 and is now int.Parse(null!), and
int.TryParse(null, out _) no longer compiles at all (CS0121, ambiguous between
the string and ReadOnlySpan<byte> overloads) and is now (string?)null.

Program.cs moves to a file-scoped namespace.

Build: 0 warnings. Tests: 20/20 passing on SDK 10.0.302.
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