Good way to compare 3 array lengths in C #?

I have 3 arrays. I have to check if these arrays have the same lengths. Is there a smart and good way to compare it?

+3
source share
9 answers

Why not

if (array1.Length == array2.Length && array1.Length == array3.Length)
{
}
+10
source

If you have Narrays, you can do this:

var a1 = new int[] { 1, 2, 3 };
var a2 = new int[] { 1, 2, 3 };
var a3 = new int[] { 1, 2, 3 };
var arr = new[] { a1, a2, a3 }; // group them all in one array

// check if all arrays have the same length as the first
var test = arr.All(x => x.Length == a1.Length);
+5
source

bool EqualLengths = Arr1.Length == Arr2.Length && Arr2.Length == Arr3.Length; A doubt that is considered smart and enjoyable, though, but I don't think something is better!

+4
source
object[] a, b, c;
return (a.Length == b.Length && b.Length == c.Length);
+4
source

simpler than if (array1.Length == array2.Length && array1.Length == array3.Length)?

+2
source
bool success = false;

var arr1 = new[] { "1", "2", "3" };
var arr2 = new[] { "1", "2", "3" };
var arr3 = new[] { "1", "2", "3" };

if (arr1.Length == arr2.Length)
{
  if (arr2.Length == arr3.Length)
  {
     success = true;
  }
}
+1
source

plain

 if (array.Length == array1.Length &&  array1.Length== array2.Length)
{
 // do something
}
0
source
// compares all arrays passed in for equal length.
// requires at least two arrays so that comparison can be made, 
// otherwise this operation would not matter
bool AreEqualLength(Array a1, Array a2, params Array[] arrs) {
  if (a1.Length != a2.Length) return false;
  foreach(var arr in arrs)
    if (arr.Length != a1.Length)
      return false;
  return true;
}
0
source
        int[] a1, a2, a3;
        a1 = new int[3];
        a2 = new int[1];
        a3 = new int[1];

        bool equalLength = (a1.Length == (a2.Length & a3.Length));
-1
source

All Articles