Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two .NET Array objects

Tags:

arrays

.net

I am trying to compare two .NET arrays. Here is an obvious implementation for comparing arrays of bytes:

bool AreEqual(byte[] a, byte[] b){
    if(a.Length != b.Length)
        return false;
    for(int i = 0; i < a.Length; i++)
        if(a[i] != b[i])
            return false;

    return true;
}

A more refined approach can be seen here (via Google).

  1. What is the simplest way (using less code but readable) to compare two .NET arrays?
  2. What is the most efficient way compare two .NET arrays?
like image 421
Agnel Kurian Avatar asked Sep 10 '25 16:09

Agnel Kurian


1 Answers

You could use SequenceEqual:

string[] a = { "1", "2", "3" };
string[] b = { "1", "2", "3" };

bool areEqual = a.SequenceEqual(b); // true


string[] c = { "1", "2", "5" };
areEqual = a.SequenceEqual(c);      // false
like image 170
Christian C. Salvadó Avatar answered Sep 13 '25 07:09

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!