English 中文(简体)
比较3个阵列长度的方法?
原标题:Nice way to compare 3 array lengths in c#?
  • 时间:2011-05-06 10:22:16
  •  标签:
  • c#
  • arrays

我有3个阵列。 我必须检查这些阵列是否长度相同。 是否有cl和冰 比较?

问题回答

为什么

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

如果有<代码>。 注

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

超过if(array1.Length = 阵列2.Length &&阵列1.Length = 阵列3.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;
  }
}

简单

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签