Is there a collection of value types in C #?

Are there collections with value type semantics in C #? So what is set1 equal to set2 if they contain the same structures / primitives? Perhaps in the same order.

+4
source share
1 answer

HashSet is pretty close, but ==does not compare values ​​in collections. SetEqualswill return true if they contain the same value. However, the order is not taken into account. You can use SequenceEqualif order is important.

  static void Main(string[] args)
  {
     HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
     HashSet<int> set2 = new HashSet<int> { 2, 1, 3 };
     HashSet<int> set3 = new HashSet<int> { 1, 2, 3 };
     Console.WriteLine(set1.SetEquals(set2));          // True
     Console.WriteLine(set1.SequenceEqual<int>(set2)); // False
     Console.WriteLine(set1.SequenceEqual<int>(set3)); // True
  }
+5
source

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


All Articles