Is there a readonly ISet-like interface?

How about the IImmutableSet<T> interface?


Edit 17 September 2020: .NET 5.0 now offers the new interface IReadOnlySet<T>:


No, there is no IReadOnlySet<T> interface in C#. Based on the needs you've described, I think you should create your own IContains<T> interface.

public interface IContains<T>
{
    bool Contains(T item);
}

Note that this can not be added to existing collections like List<T> or HashSet<T>, and doesn't contain any other set-like operations like Count or enumeration. You might use it like:

public void TestContains<T>(IContains<T> container, T item)
{
    if (container.Contains(item))
    {
        //something
    }
    else
    {
        //something else
    }
}

Tags:

C#