dotnet/BenchmarkDotNet · error · ArgumentException
Property values can not contain null.
Error message
Property values can not contain null.
What it means
Thrown by CreateArgumentTextRepresentation when building an MSBuild-style /p:{name}={value} argument string and one of the provided string values in the array is null. This is a defensive precondition check: the method already guards against a null array and an empty name, then iterates each element to ensure none is null before joining them with ';'. A null element would otherwise silently produce a malformed argument like /p:Prop=a;foo; where a segment is missing.
Source
Thrown at src/BenchmarkDotNet/Jobs/Argument.cs:130
[PublicAPI]
public class MsBuildProperty : MsBuildArgument
{
public MsBuildProperty(string name, params string[] values)
: base(CreateArgumentTextRepresentation(name, values), escapeSpecialCharacters: true)
{
}
private static string CreateArgumentTextRepresentation(string name, string[] values)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Property name must be non-empty.", nameof(name));
if (values is null)
throw new ArgumentNullException(nameof(values));
for (int i = 0; i < values.Length; i++)
if (values[i] is null)
throw new ArgumentException("Property values can not contain null.", nameof(values));
string value = values.Length switch
{
0 => string.Empty,
1 => values[0],
_ => string.Join(";", values)
};
return $"/p:{name}={value}";
}
}
}
View on GitHub (pinned to b515068b61)
Solutions
- Filter out null entries before passing the array: values = values.Where(v => v != null).ToArray()
- Ensure every element is assigned a non-null string when constructing the values array programmatically.
- If nulls are semantically meaningful, decide on a sentinel (empty string) and coalesce: values.Select(v => v ?? string.Empty).ToArray()
Example fix
// before
var values = new string[] { "a", null, "b" };
var arg = Argument.CreateArgumentTextRepresentation("Prop", values);
// after
var values = new string[] { "a", null, "b" }
.Where(v => v != null)
.ToArray();
var arg = Argument.CreateArgumentTextRepresentation("Prop", values); Defensive patterns
Strategy: validation
Validate before calling
string[] values = /* ... */;
if (values != null && values.Any(v => v is null))
values = values.Where(v => v != null).ToArray();
// safe to pass now Type guard
static bool HasNoNulls(string[] values) => values != null && values.All(v => v is not null);
Prevention
- Initialize string arrays with all elements assigned, never rely on default null.
- Use LINQ .Where(v => v != null) as a pipeline step before passing arrays to argument builders.
- Enable nullable reference types (NRT) in C# to get compile-time warnings for potential nulls.
When it happens
Trigger: Calling an API that internally constructs Argument text (e.g. Job/Characteristic settings that map to MSBuild properties) and passing a string[] that contains a null element. The loop `for (int i = 0; i < values.Length; i++) if (values[i] is null)` triggers the ArgumentException with paramName 'values'.
Common situations: Building a Job with array-valued properties (like environment variables or multi-value MSBuild properties) where one slot was never assigned, default-initialized to null, or came from a LINQ Select that returned null for a missing entry. Often seen when dynamically assembling argument lists from configuration dictionaries where some keys lack values.
Related errors
- Value cannot be null. (Parameter 'key')
- Value cannot be null. (Parameter 'value')
- The '{variable.Key}' environment variables is defined twice
AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13).
Data as JSON: /api/errors/238095231ed0c3de.
Report an issue: GitHub.