Skip to content

CA2208 incorrectly issued for C# 14 extension members #51887

@BillWagner

Description

@BillWagner

Describe the bug

Use the nameof expression on the extension parameter in a C# 14 extension member and CA2208 is emited. It shouldn't be.

To Reproduce

Try this code:

public static class ExampleExtensions
{
    extension<T>(IEnumerable<T> sequence)
    {
        public IEnumerable<T> Sample(int frequency, 
            [CallerArgumentExpression(nameof(sequence))] string? message = null)
        {
            if (sequence.Count() < frequency)
                throw new ArgumentException($"Expression doesn't have enough elements: {message}", nameof(sequence));
            int i = 0;
            foreach (T item in sequence)
            {
                if (i++ % frequency == 0)
                    yield return item;
            }
        }
    }
 }

The line that throws a new ArgumentException emits CS2208. The explanation is that sequence isn't a parameter. But it is, even though it's on the extension node.

Exceptions (if any)

Further technical details

I reproduced on the VS 2026 G.A. November release.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions