Skip to content

Lambda with an anonymous parameter type gets mixed parameter typing (CS0748) #3894

Description

@sailro

Summary

When one lambda parameter has an anonymous type, ILSpy correctly omits that parameter's
unnameable type but leaves sibling parameter types explicit. C# requires lambda parameter
types to be either all explicit or all implicit, so the output fails with CS0748.

Input code

Compiled in Release targeting .NET 10.0:

using System;

public static class C
{
    private static TValue GetOrCreate<TKey, TValue>(
        TKey key,
        Func<TKey, int, TValue> factory) => factory(key, 1);

    public static int Read()
    {
        var key = new { Value = 1 };
        return GetOrCreate(key, (item, context) => item.Value + context);
    }
}

Erroneous output

public static int Read()
{
    return GetOrCreate(new
    {
        Value = 1
    }, (item, int context) => item.Value + context);
}

Recompiling this output fails:

error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit

Expected output

Because the anonymous type cannot be named, every parameter should remain implicit:

(item, context) => item.Value + context

Details

  • Product in use: ICSharpCode.Decompiler
  • Version in use: master 7554c1b0 (11.0.0.9223)
  • The repro was verified in an isolated compile, decompile, recompile round trip.
  • ExpressionBuilder.MakeParameters removes the type only from parameters whose type contains an anonymous type, leaving the remaining parameter declarations explicitly typed.
  • I am happy to contribute a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions