A BenchmarkDotNet result can compare two methods precisely while missing a more basic problem: the methods answer different questions. Before you use a result to justify a change, establish the required behavior, check both implementations against it, and define exactly which work the measurement includes.
This tutorial checks two ways to count positive integers, catches a plausible boundary bug, and turns that contract into a measurement and reporting plan. The complete example targets .NET 10 and uses only the base class library. It was compiled and run in an isolated Linux SDK container on 9 November 2026. It measures no execution times and makes no claim that either implementation is faster.
Define the work before choosing a baseline
Our contract is deliberately narrow: given a non-null integer array, return the number of elements strictly greater than zero, and leave the array unchanged. Empty arrays return zero. Negative numbers and zero do not count. Null handling is outside this example’s contract.
A loop and a LINQ predicate can implement that contract. A method that counts zero as positive cannot be a substitute, regardless of its timing. Checking the methods only against each other would also be weak: both could share a bug. Instead, each case below has an explicit expected answer. For more on choosing assertions, see designing C# unit tests around observable behavior. The LINQ introduction explains the query approach.
Run a correctness preflight with a negative control
Create a console project, replace its entire Program.cs with the program below, then run it. These commands are for the correctness example, not a benchmark:
dotnet new console --framework net10.0 --name CountCheck
cd CountCheck
dotnet run
using System;
using System.Linq;
var cases = new (string Name, int[] Data, int Expected)[]
{
("empty", Array.Empty<int>(), 0),
("negative", new[] { -3, -1 }, 0),
("zeros", new[] { 0, 0 }, 0),
("positive", new[] { 1, 2, 3 }, 3),
("mixed", new[] { -2, 0, 4, 0, 7 }, 2),
("limits", new[] { int.MinValue, 0, int.MaxValue }, 1)
};
foreach (var item in cases)
{
int[] before = (int[])item.Data.Clone();
AssertEqual(item.Expected, CountLoop(item.Data), item.Name + " loop");
AssertEqual(item.Expected, CountLinq(item.Data), item.Name + " LINQ");
if (!item.Data.SequenceEqual(before))
throw new InvalidOperationException("Input changed: " + item.Name);
Console.WriteLine("PASS " + item.Name);
}
bool rejected = false;
try
{
AssertEqual(0, CountWrong(new[] { 0, 0 }), "zero boundary");
}
catch (InvalidOperationException error)
{
rejected = true;
Console.WriteLine("REJECTED " + error.Message);
}
if (!rejected)
throw new InvalidOperationException("Negative control escaped detection.");
Console.WriteLine("Correctness checked; no performance measured.");
static int CountLoop(int[] data)
{
int count = 0;
foreach (int value in data)
if (value > 0)
count++;
return count;
}
static int CountLinq(int[] data) => data.Count(value => value > 0);
static int CountWrong(int[] data) => data.Count(value => value >= 0);
static void AssertEqual(int expected, int actual, string label)
{
if (expected != actual)
throw new InvalidOperationException(
$"{label}: expected {expected}, actual {actual}");
}
Expected standard output, asserted exactly during verification:
PASS empty
PASS negative
PASS zeros
PASS positive
PASS mixed
PASS limits
REJECTED zero boundary: expected 0, actual 2
Correctness checked; no performance measured.
Both correct implementations pass six cases, including empty input, zeros, mixed signs and integer limits. The program snapshots each input and checks that the methods collectively leave it unchanged. These checks cover the listed cases; they do not prove equivalence for every possible input.
The negative control calls the intentionally wrong method with two zeros. The assertion expects zero, observes two and throws. The catch records that rejection. If the assertion ever accepts this wrong answer, the program throws a different exception and fails. A normal successful exit therefore means the harness detected this particular defect as well as accepting the correct cases.
Each execution owns its in-memory inputs. No files, database, network, shared mutable state or cleanup fixtures are needed. The check prints results and creates input snapshots; those operations belong to the correctness harness and must not accidentally become the operation you intend to benchmark.
Carry the same contract into BenchmarkDotNet
Microsoft’s performance diagnosis guidance recommends BenchmarkDotNet to handle measurement details such as warmup and iteration selection. That machinery helps measure the chosen methods; our correctness preflight establishes what those methods are supposed to compute.
To turn this comparison into a benchmark, use a separate console benchmark project and follow Microsoft’s BenchmarkDotNet setup guide. The guide describes benchmark method attributes, a runner and Release builds; its Visual Studio profiling integration has additional package and platform prerequisites. Those integration requirements do not apply to the BCL correctness program above.
- Move the two correct counting methods into code callable from the benchmark project. Give each a benchmark wrapper that returns its integer result. Keep the assertion helper, negative control and console output in the correctness project.
- Choose one implementation as the comparison baseline. Feed both wrappers the same array for each input case. Record array length and the distribution of negative, zero and positive values; two arrays of equal length can represent different workloads.
- Decide whether input creation belongs in the measured operation. To compare counting already available data, prepare that data before the measured calls. If your question includes constructing the array, include that work consistently in both alternatives and describe the broader operation.
- Run the benchmark in Release configuration and retain the full generated report and warnings. Record the actual BenchmarkDotNet package version, SDK, runtime, operating system, CPU and benchmark job configuration. A target framework alone does not identify the runtime that executed a benchmark.
- Repeat the comparison with sizes and value distributions relevant to your application. The six tiny correctness cases are boundary checks, not a representative performance workload.
These are the proposed measurement steps; no timed BenchmarkDotNet job or package restore was run for this tutorial. The observed verification used the fixed sandbox helper’s Debug configuration. Its successful execution establishes the assertions above, not Release performance or compatibility of a particular BenchmarkDotNet package.
Read the report as evidence for a bounded claim
First inspect the benchmark identity, input parameters, units and baseline. Keep variability columns such as Error and StdDev beside Mean when sharing a report. A smaller mean alone is not enough to establish a useful, repeatable improvement. If the difference matters to a decision, repeat under the intended conditions and investigate unstable results rather than publishing a ranking from one selected run.
The following review questions are a reporting checklist, not new measurements:
| Question | What to record |
|---|---|
| Did both methods do equivalent work? | The positive-number contract, expected results and the negative control. |
| What was timed? | Counting alone or input creation plus counting; any logging, setup or cleanup inside the method. |
| What does the comparison cover? | Exact input sizes and distributions, build, runtime, machine and job settings. |
| What supports the conclusion? | The complete report, variability, warnings and repeat runs; allocation data if collected. |
| What remains untested? | Application request latency, concurrent load and deployment behavior unless separately measured. |
Measurement boundaries matter even when the tool is working correctly. Microsoft’s EF Core example explicitly notes that context construction and disposal inside its benchmark methods are included in the reported operation. That is a useful general lesson for this array comparison: describe the work your wrappers actually perform, including any work beyond counting.
If allocation behavior matters, Microsoft’s setup guide describes MemoryDiagnoser. Keep an allocation observation distinct from a time observation; neither one alone establishes application capacity. Likewise, this sequential in-memory operation provides no evidence about request contention or concurrent throughput.
Write the claim only after the measurement exists
A useful report identifies the chosen method, the operation and inputs, the recorded environment, the observed difference and variability, and the decision it supports. Say where the result applies and what requires a separate application test. Leave numeric fields empty until an actual retained run supplies them.
For this tutorial, the supported conclusion is narrower: both counting implementations returned the six expected answers without changing the inputs, and the harness rejected the zero-boundary defect. Use that preflight before collecting BenchmarkDotNet measurements; use the resulting measurements, rather than an assumption about loops or LINQ, to decide whether a change is worthwhile.