> For the complete documentation index, see [llms.txt](https://docs.shouldly.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.shouldly.org/documentation/equality/shouldbe-1.md).

# Enumerable

```cs
var apu = new Person { Name = "Apu" };
var homer = new Person { Name = "Homer" };
var skinner = new Person { Name = "Skinner" };
var barney = new Person { Name = "Barney" };
var theBeSharps = new List<Person> { homer, skinner, barney };
theBeSharps.ShouldBe(new[] { apu, homer, skinner, barney });
```

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/EnumerableShouldBeExamples.ShouldBe.codeSample.approved.cs#L1-L6) <sup>|</sup> [<sup>anchor</sup>](#snippet-EnumerableShouldBeExamples.ShouldBe.codeSample.approved.cs)

**Exception**

```
theBeSharps
    should be
[Apu, Homer, Skinner, Barney]
    but was
[Homer, Skinner, Barney]
    difference
[*Homer*, *Skinner*, *Barney*, *]
```

## Dictionaries and sets

The order a dictionary or a set enumerates in isn't part of its value, so when both sides are dictionaries (`IDictionary<TKey, TValue>`, `IReadOnlyDictionary<TKey, TValue>` or `IDictionary`) or sets (`ISet<T>` or `IReadOnlySet<T>`), `ShouldBe` and `ShouldNotBe` compare them in any order. A `Dictionary` equals a `SortedDictionary` with the same entries, and a `HashSet` equals a `SortedSet` with the same elements. This applies at any depth, so a dictionary inside a list, or a set inside a dictionary, is compared the same way.

The same goes when only one side is a dictionary or a set, and the other a list, an array or a LINQ query. A `HashSet` has no order for the list's to be compared with:

```cs
new HashSet<int> { 3, 2, 1 }.ShouldBe([1, 2, 3]);                           // passes
set.Where(x => x > 0).ShouldBe(set);                                        // passes
new Dictionary<string, int> { ["a"] = 1, ["b"] = 2 }
    .ShouldBe([new("b", 2), new("a", 1)]);                                  // passes
```

A sorted or ordered collection is the exception. A `SortedSet`, `SortedDictionary`, `SortedList`, `ImmutableSortedSet`, `ImmutableSortedDictionary` or `OrderedDictionary`, or any dictionary or set that can be indexed by position, keeps an order that a test may be about, so against a list or an array it is compared in order:

```cs
var newestFirst = new SortedSet<int>(Comparer<int>.Create((x, y) => y.CompareTo(x))) { 1, 2, 3 };

newestFirst.ShouldBe([3, 2, 1]);                                            // passes
newestFirst.ShouldBe([1, 2, 3]);                                            // fails: compared in order
```

A dictionary's entries are matched by key, and their values compared, each the way `ShouldBe` compares a single value, as `ShouldBeEquivalentTo` matches them. Neither dictionary's own key comparer takes part, so a dictionary created with `StringComparer.OrdinalIgnoreCase` holding `"A"` doesn't equal one holding `"a"`. The failure message lists each key that is missing, isn't expected, or has a different value:

```cs
var duffStock = new Dictionary<string, int> { ["Duff"] = 12, ["Duff Lite"] = 6, ["Duff Dry"] = 3 };
duffStock.ShouldBe(new Dictionary<string, int> { ["Duff Dry"] = 3, ["Duff"] = 24, ["Raspberry Duff"] = 1 });
```

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/EnumerableShouldBeExamples.ShouldBeDictionary.codeSample.approved.cs#L1-L2) <sup>|</sup> [<sup>anchor</sup>](#snippet-EnumerableShouldBeExamples.ShouldBeDictionary.codeSample.approved.cs)

**Exception**

```
duffStock
    should be
[["Duff Dry" => 3], ["Duff" => 24], ["Raspberry Duff" => 1]]
    but was
[["Duff" => 12], ["Duff Lite" => 6], ["Duff Dry" => 3]]
    difference
["Duff"] should be 24 but was 12
["Raspberry Duff"] is missing
["Duff Lite"] is not expected
```

A set's elements are matched one for one, also when you pass a `Case` for strings, a tolerance for numbers, or a comparer:

```cs
var bowlingTeam = new HashSet<string> { "Homer", "Moe", "Otto" };
bowlingTeam.ShouldBe(new HashSet<string> { "Apu", "Homer", "Moe" });
```

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/EnumerableShouldBeExamples.ShouldBeSet.codeSample.approved.cs#L1-L2) <sup>|</sup> [<sup>anchor</sup>](#snippet-EnumerableShouldBeExamples.ShouldBeSet.codeSample.approved.cs)

**Exception**

```
bowlingTeam
    should be
["Apu", "Homer", "Moe"]
    but was (case sensitive comparison)
["Homer", "Moe", "Otto"]
    difference
"Apu" is missing
"Otto" is not expected
```

The views a dictionary gives of its keys and values (`dictionary.Keys`, `dictionary.Values`) are neither dictionaries nor sets, so against a list they are compared in order. Pass `ignoreOrder: true` to compare them in any order.
