An easy way to iterate over all unique pairs in a collection

I have a C # 3 object HashSetcontaining several elements. I want to check something between each pair of them without repeating [(a, b) = (b, a)] and without linking the element to itself. I was thinking of switching to some List, so I can associate each item with all of its following items. Is it possible to do something similar with a common, disordered Collection,? Or IQuaryable?

+3
source share
2 answers

It would be easier if you could access them with an index, and although there is an extension ElementAt <>, it is probably faster to use .ToList ().

When you have an address list:

for (int i = 0; i < list.Count-1; i++)
  for (int j = i+1; j < list.Count; j++)
     Foo(list[i], list[j]);
+3

Source: https://habr.com/ru/post/1713748/


All Articles