compare a list of integers

T

Tem

List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<int> b = new List<int>();
b.Add(1);
b.Add(2);
b.Add(3);

I need to compare two integer lists.
It returns true if values and ordering are the same.
Is there a better way to do this than a foreach loop?

Tem
 
J

Jon Skeet [C# MVP]

Tem said:
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<int> b = new List<int>();
b.Add(1);
b.Add(2);
b.Add(3);

I need to compare two integer lists.
It returns true if values and ordering are the same.
Is there a better way to do this than a foreach loop?

Which version of .NET are you using? .NET 3.5 has an extension method
on IEnumerable<T> of SequenceEqual:

if (a.SequenceEqual(b))
....
 
M

Marc Gravell

but I thought extension methods would make things slow? been trying to avoid

Why would you think that? OK, in reality you could shave a few cycles
if you know you have List<T> by checking the length first
(SequenceEqual doesn't do this, although this could be a missed
opportunity - it could check if both operands implement IList, and
compare the length) - and because it is using the interface (rather
than direct to the sealed List<T>) it won't inline any of the fetches
- but this is rarely the most significant thing.

My advice: use the simplest code that clearly expresses what you want
to do. SequenceEqual does this; *if* you later find this is a
bottleneck, then tweak it (perhaps introduce a List<T> SequenceEqual
extension method) - but until then you're optimising prematurely.

Marc
 
M

Marc Gravell

For reference - an additional overload that checks a few obvious cases
first... just add your namespace as a "using" at the top, and the
overload resolution will select it instead of
Enumerable.SequenceEqual, since ICollection<T> is more specific than
IEnumerable<T>

Adding a List<T>-specific version would almost certainly be overkill;
I doubt you'd ever see any tangible performance benefit in most code.

Marc

namespace SomeNamespace
{
public static class CollectionExt
{
public static bool SequenceEqual<T>(this ICollection<T> first,
ICollection<T> second)
{
return SequenceEqual<T>(first, second,
EqualityComparer<T>.Default);
}
public static bool SequenceEqual<T>(this ICollection<T> first,
ICollection<T> second, IEqualityComparer<T> comparer)
{
if (first == null) throw new
ArgumentNullException("first");
if (second == null) throw new
ArgumentNullException("second");
if (ReferenceEquals(first, second)) return true;
if (first.Count != second.Count) return false;
return System.Linq.Enumerable.SequenceEqual<T>(first,
second, comparer);
}
}
}
 
J

Jon Skeet [C# MVP]

Tem said:
Something tells me this will be much more efficient.

Certainly in the special cases, where the two lists are either
"obviously" different or "obviously" the same.

But note that it's still an extension method - your earlier comment
about how you've been "trying to avoid [extension methods]" should be
reconsidered, IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Merge lists 8
list question 2
Inconsistent accessibility 3
b-tree 11
copy lists 1
complex List<> sorting 5
what is the wrong with this implementation of Comparable<T> 4
Generic method & constraint 10

Top