开发者

Nice way to compare 3 array lengths in c#?

开发者 https://www.devze.com 2023-03-03 02:28 出处:网络
开发者_JS百科I have 3 arrays. I have to check whether those arrays have the same length. Is there a clever and nice way to compare it?Why not

开发者_JS百科I have 3 arrays. I have to check whether those arrays have the same length. Is there a clever and nice way to compare it?


Why not

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


If you have N arrays you could 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);


bool EqualLengths = Arr1.Length == Arr2.Length && Arr2.Length == Arr3.Length; Doubt that counts as clever and nice though, but don't think there's anything better!


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


more simple than 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;
  }
}


simple

 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));
0

精彩评论

暂无评论...
验证码 换一张
取 消