C# SequenceEqual Method

Array elements

Are two sequences are the same? With the SequenceEqual extension from System.Linq in the C# language, you can test two collections for equality in one statement. We look at the SequenceEqual method.

Key points: SequenceEqual compares two collections for exact equality. Its performance is much worse than alternative implementations.

Example

The example program allocates four string arrays on the managed heap. Next, the SequenceEqual method is used on the arrays: array1 is tested against array2, array3, and array4.

This C# example program uses the SequenceEqual extension method. It requires System.Linq.

Program that uses SequenceEqual [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	string[] array1 = { "dot", "net", "perls" };
	string[] array2 = { "a", "different", "array" };
	string[] array3 = { "dot", "net", "perls" };
	string[] array4 = { "DOT", "NET", "PERLS" };

	bool a = array1.SequenceEqual(array2);
	bool b = array1.SequenceEqual(array3);
	bool c = array1.SequenceEqual(array4, StringComparer.OrdinalIgnoreCase);

	Console.WriteLine(a);
	Console.WriteLine(b);
	Console.WriteLine(c);
    }
}

Output

False
True
True

Results. The variable array1 was determined to be not equal to array2. Array1 was equal to array3, however. Finally, array1 was equal to array4 when the IEqualityComparer implementation (StringComparer.OrdinalIgnoreCase) was passed as the second argument. The character casing in this situation was ignored.

Performance

Performance optimization

Methods found in System.Linq usually have worse performance on arrays than custom imperative algorithms. I tested the performance of SequenceEqual because I had nothing better to do. I compared two equal arrays of 32767 elements. SequenceEqual was more than ten times slower than the imperative algorithm (ArraysEqual).

Program that benchmarks SequenceEqual [C#]

using System;
using System.Diagnostics;
using System.Linq;

class Program
{
    static void Main()
    {
	const int max = 10000;
	var a1 = Enumerable.Range(0, short.MaxValue).ToArray();
	var a2 = Enumerable.Range(0, short.MaxValue).ToArray();

	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < max; i++)
	{
	    bool equals = a1.SequenceEqual(a2);
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < max; i++)
	{
	    bool equals = ArraysEqual(a1, a2);
	}
	s2.Stop();
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
	    max).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
	    max).ToString("0.00 ns"));
	Console.Read();
    }

    static bool ArraysEqual(int[] a1, int[] a2)
    {
	if (a1.Length == a2.Length)
	{
	    for (int i = 0; i < a1.Length; i++)
	    {
		if (a1[i] != a2[i])
		{
		    return false;
		}
	    }
	    return true;
	}
	return false;
    }
}

Result

515889.45 ns
 36280.79 ns

Note: It's good to keep in mind that SequenceEqual on large equal arrays is more than ten times slower than a loop. Although I have been tempted to use SequenceEqual in this sort of situation, as to compare byte arrays, it really hinders performance.

Summary

The C# programming language

The SequenceEqual extension method in the C# language is one of the easiest ways to compare two arrays or other collections such as Lists for equality. Any argument to SequenceEqual must implement the IEnumerable interface, which reduces the range of compatible types you can use it upon.

LINQ Examples
.NET