Computer >> Computer tutorials >  >> Programming >> C#

SequenceEqual method in C#


The SequenceEqual method is used to test collections for equality.

Let us set three string arrays −

string[] arr1 = { "This", "is", "it" };
string[] arr2 = { "My", "work", "report" };
string[] arr3 = { "This", "is", "it" };

Now, compare the first array with the second using the SequenceEqual() method −

arr1.SequenceEqual(arr2);

The following is an example −

Example

using System;
using System.Linq;
class Program {
   static void Main() {
      string[] arr1 = { "This", "is", "it" };
      string[] arr2 = { "My", "work", "report" };
      string[] arr3 = { "This", "is", "it" };
      bool res1 = arr1.SequenceEqual(arr2);
      Console.WriteLine(res1);
      bool res2 = arr1.SequenceEqual(arr3);
      Console.WriteLine(res2);
   }
}

Output

False
True