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?
Why not
if (array1.Length == array2.Length && array1.Length == array3.Length) { }
If you have Narrays, you can do this:
N
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);
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!
bool EqualLengths = Arr1.Length == Arr2.Length && Arr2.Length == Arr3.Length;
object[] a, b, c; return (a.Length == b.Length && b.Length == c.Length);
simpler than if (array1.Length == array2.Length && array1.Length == array3.Length)?
if (array1.Length == array2.Length && array1.Length == array3.Length)
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; } }
plain
if (array.Length == array1.Length && array1.Length== array2.Length) { // do something }
// 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; }
int[] a1, a2, a3; a1 = new int[3]; a2 = new int[1]; a3 = new int[1]; bool equalLength = (a1.Length == (a2.Length & a3.Length));