c# check if two collections contain the same entries code example

Example 1: c# how to check if two lists have same values

var list1 = new List<int> { 1, 2, 3, 1 };
var list2 = new List<int> { 2, 1, 3, 1 };
var list3 = new List<int> { 2, 2, 3, 2 };
bool areTheSame1 = list1.SequenceEqualsIgnoreOrder(list2); //True
bool areTheSame2 = list1.SequenceEqual(list2); //True
bool areTheSame3 = list1.SequenceEqual(list3); //False

Example 2: check two lists are equal c#

List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 1, 2, 3 };

if (list1.SequenceEqual(list2))
{
  Console.WriteLine("true");
}
else
{
  Console.WriteLine("false");
}