0

I want to make sure that all items in a collection to be different. How to do that?

Let's say:

IEnumerable collection = new[] { 1, 2, 5, 8 };

collection.Should().BeAllDifferent(); // NOTE: BeAllDifferent doesn't exist
mousetail
  • 7,009
  • 4
  • 25
  • 45
  • 1
    There's nothing built-in to FA to do that check for you. You can perform the logic in the link above to check if the distinct count is equal to the original count, or make your own custom extension method (which does the same thing) – gunr2171 Nov 04 '21 at 14:51
  • The linked solution is perfectly fine (and kinda neat), thanks. It basically turns the problem upside down. – Albert Takács Nov 04 '21 at 14:55
  • @gunr2171, The linked question helps, but it doesn't answer it, which is why you had to add to actually answer it in your second comment. Please don't vote to close questions which require a comment or answer to answer because the other Q&A doesn't actually answer the question! – ikegami Nov 04 '21 at 14:57
  • Link to original dupe target for reference: https://stackoverflow.com/questions/18303897/test-if-all-values-in-a-list-are-unique?noredirect=1&lq=1 – mousetail Nov 09 '21 at 10:29

1 Answers1

0

Try using

collection.Should().OnlyHaveUniqueItems().And.Should().BeOfType<int>();
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Jeremy Caney Mar 15 '23 at 00:15