Skip to content

ImmutableHashSet properties allows collection initializer usage without error but initializer has no effect #110279

@ionite34

Description

@ionite34

Description

(System.Collections.Immutable) ImmutableHashSet currently allow collection initializer usage when it is a property, and causes no compilation errors, however the resulting property retains the original assignment.

Reproduction Steps

using System.Collections.Immutable;

var x = new Thing
{
    Set =
    {
        "Abc",
        "123"
    }
};

Console.WriteLine(x.Set.Count); // 0

public class Thing
{
    public ImmutableHashSet<string> Set { get; set; } = ImmutableHashSet<string>.Empty;
}

Expected behavior

One would either expect the collection initializer to make the assignments or, like in the case of ImmutableDictionary, cause a Error CS0200 : Property or indexer 'ImmutableDictionary<string, string>.this[string]' cannot be assigned to -- it is read only error, instead of allowing the initializer while not having an effect.

Actual behavior

Collection retains the default value assigned and the collection initializer has no effect.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 9 and .NET 8, Windows x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions