A few examples can show that a parser handles familiar inputs. A property asks whether a relationship holds across many generated cases. For integer parsing, a useful starting relationship is: format a value as invariant decimal text, parse that text, and recover the original value.
This tutorial builds a small property-based test harness using only the .NET base class library. It exposes a deliberate parsing defect, checks the corrected wrapper, and adds examples for behavior the property cannot cover. The complete program was compiled and run targeting .NET 10 in an isolated Linux SDK container on November 8, 2026. No test-framework package is required.
Define the parsing contract before generating inputs
Our application wrapper accepts signed decimal integers using NumberStyles.Integer and CultureInfo.InvariantCulture. It allows a leading sign and surrounding whitespace. Empty, malformed and out-of-range text must fail; this contract excludes decimal points and thousands separators. The Boolean return value determines success. See Microsoft’s Int32.TryParse string/style/provider overload for the style, provider and failure rules.
The property has two assertions: parsing must succeed, and the resulting integer must equal the input value. Merely checking that parsing does not throw would miss a parser that returns false for every input. Checking only the resulting number would mishandle zero.
The formatter uses Int32.ToString(IFormatProvider) with the same explicit culture as the parser. That makes this test’s numeric text independent of the machine’s current culture. It tests an invariant-format contract, not every localized number format.
Run the complete property harness
Create a console project with a .NET 10 SDK, replace its entire Program.cs with the program below, and run it. The template’s default language settings are sufficient; there are no external dependencies.
dotnet new console --framework net10.0 --name ParsingProperties
cd ParsingProperties
dotnet run
Cases supplies five deliberate boundary values followed by 2,000 values from a fixed arithmetic sequence. The unchecked operations make the generator’s wraparound explicit. This is reproducible sampling, with no claim that it is statistically uniform. Both parser checks enumerate the same sequence from the same starting state.
using System;
using System.Collections.Generic;
using System.Globalization;
static class Program
{
delegate bool Parser(string? text, out int value);
static bool ParseInteger(string? text, out int value) =>
int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out value);
// Deliberate defect: this style rejects the sign on negative integers.
static bool BrokenParser(string? text, out int value) =>
int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out value);
static IEnumerable<int> Cases()
{
yield return 0;
yield return 1;
yield return -1;
yield return int.MinValue;
yield return int.MaxValue;
uint state = 0x12345678u;
for (int i = 0; i < 2000; i++)
{
state = unchecked(state * 1664525u + 1013904223u);
yield return unchecked((int)state);
}
}
static bool RoundTrips(Parser parser, int input)
{
string text = input.ToString(CultureInfo.InvariantCulture);
return parser(text, out int actual) && actual == input;
}
static void Require(bool condition, string message)
{
if (!condition)
throw new InvalidOperationException(message);
}
static void Main()
{
int? counterexample = null;
foreach (int input in Cases())
{
if (!RoundTrips(BrokenParser, input))
{
counterexample = input;
break;
}
}
Require(counterexample == -1, "The property must expose the deliberate defect.");
Console.WriteLine("Broken parser: counterexample -1");
int checkedCount = 0;
foreach (int input in Cases())
{
Require(RoundTrips(ParseInteger, input),
"Round-trip failed for " + input.ToString(CultureInfo.InvariantCulture));
checkedCount++;
}
Require(checkedCount == 2005, "Unexpected case count.");
Console.WriteLine("Correct parser: 2005 round trips passed");
(string Text, int Expected)[] accepted =
{
("+42", 42), (" 42 ", 42), ("00042", 42), ("-0", 0)
};
foreach (var item in accepted)
Require(ParseInteger(item.Text, out int value) && value == item.Expected,
"Accepted-input contract failed: " + item.Text);
Console.WriteLine("Accepted forms: 4 passed");
string?[] rejected =
{
null, "", "abc", "1.5", "1,000", "2147483648", "-2147483649", "--1"
};
foreach (string? text in rejected)
Require(!ParseInteger(text, out _), "Unexpected accepted input: " + text);
Console.WriteLine("Rejected forms: 8 passed");
}
}
Read the failure and passing results
The asserted application stdout is exactly:
Broken parser: counterexample -1
Correct parser: 2005 round trips passed
Accepted forms: 4 passed
Rejected forms: 8 passed
BrokenParser deliberately uses NumberStyles.None, which rejects a negative sign. The first two values, zero and one, round-trip. The third, minus one, does not. The harness requires that exact counterexample, so the demonstration itself fails if it stops detecting the defect. It does not minimize failing inputs: minus one is simply the first failing value in this sequence.
ParseInteger uses the intended integer style. Its check visits all 2,005 cases, including both int limits. Each assertion requires success and equality. Unexpected behavior throws and causes an unsuccessful run. The overall program succeeds because finding the deliberate defect is an expected assertion; the first output line is not an unhandled test failure.
The generator restarts on each enumeration rather than sharing mutable state between tests. All test data stays in memory, and the program changes no culture settings or files, so it has no external resources to clean up. For more on the enumeration mechanism, read iterator methods and repeated reads.
Cover what round trips leave out
Formatting an integer does not produce a leading plus sign, surrounding spaces or redundant leading zeroes. The four accepted-form cases therefore check those inputs separately, including the alternative spelling -0. A parser could satisfy the round-trip property while incorrectly rejecting all four.
The eight rejected-form cases check null, empty text, letters, decimal punctuation, a thousands separator, both overflow directions and a repeated sign. These are explicit examples, not an exhaustive malformed-string generator. The test intentionally ignores the output value after a failed parse because this application’s failure contract depends only on the Boolean result.
There is also an oracle limitation: this wrapper delegates to the BCL parser, and the formatter comes from the same platform. The property is useful for detecting wrapper mistakes such as the wrong style; it is not an independent verification of the .NET parsing implementation. Passing 2,005 cases does not establish the relationship for every 32-bit integer, every string, another target runtime or a custom grammar.
When adapting this harness to an application parser, retain the seed and a failing value in the failure report, promote discovered counterexamples into named regression cases, and generate inputs for the actual grammar. This small harness has no automatic shrinking or test-runner discovery. Use it to make the property and its blind spots visible before adding those capabilities. For the conversion basics, see converting a string to an int in C#.