Skip to content

Please add function overloads for IReadOnlyCollection #7956

Closed

Description

@RioMcBoo

ICollection is very rarely implemented in custom types because, for syntactic sugar, it's sufficient to implement the Add(TValue item) method with any return type.

  1. It's usually very convenient to implement bool Add(TValue item) because its operation is easy to control without having to first use bool Contains(TValue item). Otherwise, you'll encounter exceptions. In any case, this reduces code performance because bool Contains(TValue item) is essentially already implemented in the bool Add(TValue item) method under the hood. You're probably familiar with the slowness of exceptions.

  2. Often, validation methods such as CollectionAssert.AreEqual and similar methods don't change anything in collections anyway. Therefore, choosing ICollection as the argument type isn't a good idea. (I assume this was done to support legacy versions of C#.)

Activity

  1. MihuBot commented on Apr 30, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  2. added
    area/assertion
    and removed
    needs/triage
    on May 15, 2026
  3. Evangelink commented on May 20, 2026

    @Evangelink
    Member

    Closing as superseded — the modern Assert.* family added in 4.x already accepts IEnumerable<T> (which is strictly broader than IReadOnlyCollection<T> and ICollection<T>), so no extra CollectionAssert overloads are needed. The recommended replacements are all on Assert itself:

    CollectionAssert.* Modern Assert.* replacement (takes IEnumerable<T>)
    AreEqual (ordered) Assert.AreSequenceEqual<T>(expected, actual) (#8334)
    AreNotEqual (ordered) Assert.AreNotSequenceEqual<T>(notExpected, actual) (#8334)
    AreEqual (any order) Assert.AreSequenceEqual<T>(expected, actual, SequenceOrder.InAnyOrder) (#8334)
    AreEquivalent / structural Assert.AreEquivalent<T> / AreNotEquivalent<T> (#8266)
    Contains Assert.Contains<T>(item, collection) (#8265)
    DoesNotContain Assert.DoesNotContain<T>(item, collection) (#8265)
    AllItemsAreNotNull Assert.AreAllNotNull<T>(collection) (#8237)
    AllItemsAreUnique Assert.AreAllDistinct<T>(collection) (#8237)
    AllItemsAreInstancesOfType Assert.AreAllOfType<T>(collection) (#8237)
    IsSubsetOf Assert.ContainsAll<T>(expected, collection) (#8234)
    IsNotSubsetOf Assert.DoesNotContainAll<T>(expected, collection) (#8234)
    count check Assert.HasCount<T>(n, collection) / IsEmpty / IsNotEmpty (#8262)

    All overloads use RFC 012 structured failure messages and apply to any IReadOnlyCollection<T>, custom enumerable, or LINQ query without needing .ToList() / casts.

    For the umbrella analyzer guidance pushing users toward these APIs (extension of MSTEST0037), see #8338.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/assertion

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions