Comparing two string arrays in C#

Wes Field picture Wes Field · Jun 20, 2013 · Viewed 59.3k times · Source

Say we have 5 string arrays as such:

string[] a = {"The","Big", "Ant"};
string[] b = {"Big","Ant","Ran"};
string[] c = {"The","Big","Ant"};
string[] d = {"No","Ants","Here"};
string[] e = {"The", "Big", "Ant", "Ran", "Too", "Far"};

Is there a method to compare these strings to each other without looping through them in C# such that only a and c would yield the boolean true? In other words, all elements must be equal and the array must be the same size? Again, without using a loop if possible.

Answer

Ahmed KRAIEM picture Ahmed KRAIEM · Jun 20, 2013

You can use Linq:

bool areEqual = a.SequenceEqual(b);