Skip to content

Unexpected nullable warning in object initializer member using extension Add method #81102

@RikkiGibson

Description

@RikkiGibson

Version Used:

Steps to Reproduce: .NET Lab

#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;

class Program
{
    static void Main()
    {
        IEnumerable<string> items = [];
        _ = new C()
        {
            Items = { items } // unexpected warning CS8620: Argument of type 'IEnumerable<string>' cannot be used for parameter 'list' of type 'List<string>' in 'Ext.extension<string>(List<string>)' due to differences in the nullability of reference types.
        };
    }
}

class C
{
    public List<string> Items { get; set; } = [];
}

static class Ext
{
    extension<T>(List<T> list)
    {
        public void Add(IEnumerable<T> values) => list.AddRange(values);
    }
}

Expected Behavior: No warnings are reported

Actual Behavior: A nullable warning is reported for 'items'. Note that the parameter which is being referenced is also unexpected,

The bug is may be related to the adjustments which are made to extension calls in order to include the receiver argument. We would want to ensure that the scenario behaves similarly to an classic public static void Add<T>(this List<T> list, IEnumerable<T> values) => ...; extension method.

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions