.NET Daily

C#

How to compare two list of items in C#

Posted on .

How to compare two list of items in C#

Introduction

If you want to compare two list of items in C# you can’t just use the equal sign or a loop through elements, you should use Enumerable.SequenceEqual method. For example:

List list1 = new List();

list1.Add(7);
list1.Add(5);
list1.Add(9);

List list2 = new List();

list2.Add(7);
list2.Add(5);
list2.Add(9);

if (list1.SequenceEqual(list2))
{
     //lists are identical
}
else
{
     // lists have different items
}

You should not worry about the performance, LINQ has a pretty good algorithm for that.

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

There are no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

View Comments (0) ...
Navigation