Compare two .NET Array objects

前端 未结 4 1615
猫巷女王i
猫巷女王i 2020-12-15 04:28

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 !         


        
4条回答
  •  生来不讨喜
    2020-12-15 04:55

    With the advent of .NET 4 you can use the method Equals() being provided by .NET arrays' explicitly implemented interface IStructuralEquatable. Then the code might look like this (I rewrote CMS' example):

    string[] a = { "1", "2", "3" };
    string[] b = { "1", "2", "3" };
    bool result = ((IStructuralEquatable)a).Equals(b, StructuralComparisons.StructuralEqualityComparer);
    // result evaluates to true.
    

    (IStructuralEquatable is also implemented in Tuples (also new in .NET 4).)

提交回复
热议问题