dotnet/BenchmarkDotNet · error · NotSupportedException
Cannot emit default for {resultType}.
Error message
Cannot emit default for {resultType}. What it means
IlGeneratorDefaultValueExtensions emits IL to load a default value for a benchmark method's return type; the default arm throws NotSupportedException ("Cannot emit default for {resultType}.") for types it cannot handle (it covers primitives, decimal, etc.). This determines the placeholder returned when a benchmark does not complete a measurement iteration.
Source
Thrown at src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs:156
ilBuilder.Emit(OpCodes.Ldc_I4_0);
ilBuilder.Emit(OpCodes.Conv_U);
break;
case Type t when t == typeof(double):
ilBuilder.Emit(OpCodes.Ldc_R8, 0.0d);
break;
case Type t when t == typeof(float):
ilBuilder.Emit(OpCodes.Ldc_R4, 0.0f);
break;
case Type t when t == typeof(decimal):
/*
// return decimal.Zero;
IL_0011: ldsfld valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Zero
*/
var zeroField = typeof(decimal).GetField(nameof(decimal.Zero))!;
ilBuilder.Emit(OpCodes.Ldsfld, zeroField);
break;
default:
throw new NotSupportedException($"Cannot emit default for {resultType}.");
}
}
}View on GitHub (pinned to b515068b61)
Solutions
- Change the benchmark's return type to a supported primitive or reference type (box/convert the real return value).
- Wrap the unusual result into a supported type before returning it from the benchmark method.
- Upgrade BenchmarkDotNet and report the unsupported return type if still needed.
Example fix
// before [Benchmark] public IntPtr Run() => ...; // after [Benchmark] public long Run() => (...).ToInt64();
Defensive patterns
Strategy: validation
Validate before calling
// ensure the benchmark returns a supported type before running [Benchmark] public long Run() => Compute(); // not IntPtr/ref struct
Try / catch
try { runner.Run(); }
catch (NotSupportedException ex) when (ex.Message.Contains("Cannot emit default")) { /* change return type */ } Prevention
- Return a primitive or boxed/reference type from benchmarks.
- Avoid pointers, ref structs, and exotic return types.
- Upgrade BenchmarkDotNet for expanded default-value support.
When it happens
Trigger: The benchmark method returns a type for which the emitter has no default-value case, e.g. certain pointers, typed references, unsupported generic structs, or exotic types outside the primitive/decimal set.
Common situations: Benchmarking a method whose return type is unusual (ref structs, pointers, interop types) that the default-value emitter cannot synthesize.
Related errors
- Method {methodToCall} should be static method.
- The methodToCall should have non-null DeclaringType.
- Method {methodToCall} should be instance method.
- The optionalLocalThis should be null for static calls
- Can not emit base call for {constructorBuilder}
AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13).
Data as JSON: /api/errors/c8ea9ba590c99bb6.
Report an issue: GitHub.