Skip to content

Using the Collection Initializer of HashSet results in an empty HashSet (but compiles just fine) #1018

@Malgefor

Description

@Malgefor

Hi!

When you use the collection initializer of a HashSet you end up with an empty HashSet, which I guess is the case because it is an immutable collection, but since I am creating the collection at that point in time and not modifying it after creation I was surprised to see my HashSet was empty. The thing is that this compiles just fine but at runtime the HashSet is empty. Below a comparison between the System and LangExt versions of the type:

using System.Collections.Generic;

using Shouldly;

using Xunit;

namespace Tests;

public class HashSetTest
{
    private static readonly LanguageExt.HashSet<string> HashSetLangExt =
        new()
        {
            "value1",
            "value2",
            "value3"
        };

    private static readonly HashSet<string> HashSetSystem =
        new()
        {
            "value1",
            "value2",
            "value3"
        };

    [Fact]
    public void LangExt_ShouldContainItems() => HashSetLangExt.ShouldNotBeEmpty(); // False

    [Fact]
    public void System_ShouldContainItems() => HashSetSystem.ShouldNotBeEmpty(); // True
}

I don't know if this is intended behavior or a bug, but it sure is confusing :P

(running the latest version of LangExt btw)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions