dotnet/BenchmarkDotNet · error · InvalidOperationException

Benchmark {benchmark.Name} has invalid number of defined arg

Error message

Benchmark {benchmark.Name} has invalid number of defined arguments provided with [Arguments]! {argumentsAttribute.Values.Length} instead of {parameterDefinitions.Length}.

What it means

Thrown during benchmark case generation when a [Arguments] attribute on a benchmark method provides a number of values that does not match the method's parameter count. The check `parameterDefinitions.Length != argumentsAttribute.Values.Length` ensures every [Arguments] row has exactly one value per declared parameter.

Source

Thrown at src/BenchmarkDotNet/Running/BenchmarkConverter.cs:233

        private static IEnumerable<ParameterInstances> GetArgumentsDefinitions(MethodInfo benchmark, Type benchmarkType, SummaryStyle summaryStyle)
        {
            var argumentsAttributes = benchmark.GetCustomAttributes<PriorityAttribute>();
            int priority = argumentsAttributes.Select(attribute => attribute.Priority).Sum();

            var parameterDefinitions = benchmark.GetParameters()
                .Select(parameter => new ParameterDefinition(parameter.Name!, false, [], true, parameter.ParameterType, priority))
                .ToArray();

            if (parameterDefinitions.IsEmpty())
            {
                yield return new ParameterInstances([]);
                yield break;
            }

            foreach (var argumentsAttribute in benchmark.GetCustomAttributes<ArgumentsAttribute>())
            {
                if (parameterDefinitions.Length != argumentsAttribute.Values.Length)
                    throw new InvalidOperationException($"Benchmark {benchmark.Name} has invalid number of defined arguments provided with [Arguments]! {argumentsAttribute.Values.Length} instead of {parameterDefinitions.Length}.");

                yield return new ParameterInstances(
                    argumentsAttribute
                        .Values
                        .Select((value, index) =>
                            {
                                var definition = parameterDefinitions[index];
                                var type = definition.ParameterType;
                                return new ParameterInstance(definition, Map(value, type), summaryStyle);
                            })
                        .ToArray());
            }

            if (!benchmark.HasAttribute<ArgumentsSourceAttribute>())
                yield break;

            var argumentsSourceAttribute = benchmark.GetCustomAttribute<ArgumentsSourceAttribute>()!;
            var targetType = argumentsSourceAttribute.Type ?? benchmarkType;

View on GitHub (pinned to b515068b61)

Solutions

  1. Count the benchmark method parameters and ensure each [Arguments(...)] provides exactly that many values in positional order.
  2. Use [ArgumentsSource] instead for large or dynamic parameter sets.
  3. After changing a method signature, audit all [Arguments] attributes on that method.

Example fix

// before
[Benchmark]
[Arguments(1)]
public void Run(int a, int b) { }

// after
[Benchmark]
[Arguments(1, 2)]
public void Run(int a, int b) { }
Defensive patterns

Strategy: validation

Validate before calling

int paramCount = benchmarkMethod.GetParameters().Length;
foreach (var attr in benchmarkMethod.GetCustomAttributes<ArgumentsAttribute>())
    if (attr.Values.Length != paramCount)
        throw new InvalidOperationException($"[Arguments] has {attr.Values.Length} values, expected {paramCount}.");

Prevention

When it happens

Trigger: Declaring [Benchmark] void Run(int a, string b) with [Arguments(1)] or [Arguments(1, "x", 2.0)] — any count other than exactly 2 values triggers the exception during conversion.

Common situations: Adding or removing a parameter from a benchmark method but forgetting to update the [Arguments] attributes. Using multiple [Arguments] rows where some have the wrong count. Miscounting positional arguments in the attribute.

Related errors


AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13). Data as JSON: /api/errors/e90902aa67c18f5b. Report an issue: GitHub.