compare two list c# code example
Example 1: 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");
}
Example 2: difference two list c#
var list1 = new List<int> { 1, 2, 3, 4, 5};
var list2 = new List<int> { 3, 4, 5, 6, 7 };
var list3 = list1.Except(list2);
var list4 = list2.Except(list1);
var resultList = list3.Concat(list4).ToList();
Example 3: i comparer for lists c#
using System.Collections.Generic;
using System.Linq;
namespace YourProject.Extensions
{
public static class ListExtensions
{
public static bool SetwiseEquivalentTo<T>(this List<T> list, List<T> other)
where T: IEquatable<T>
{
if (list.Except(other).Any())
return false;
if (other.Except(list).Any())
return false;
return true;
}
}
}