Skip to content

IDE0028 raised erroneously for collection type with Add(T) and IEnumerable<T[]> #72956

Closed as not planned
@stephentoub

Description

@stephentoub

Version Used:
Version 17.11.0 Preview 1.0 [34804.211.main]

Steps to Reproduce:

using System.Collections;

MyCollection<object> c = new() { "Hello" };

class MyCollection<T> : IEnumerable<T[]>
{
    public void Add(T item) { }
    public IEnumerator<T[]> GetEnumerator() => throw new NotImplementedException();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Diagnostic Id:
IDE0028

Expected Behavior:
No diagnostics.

Actual Behavior:
IDE0028 triggers and the fixer rewrites the new() { "Hello" } to be ["Hello"], which then fails to compile with error CS0029: Cannot implicitly convert type 'string' to 'object[]'.

cc: @CyrusNajmabadi

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEhelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions