简体   繁体   English

BitArray无法正常工作

[英]BitArray doesn't work as expected

I have this BitArray : 我有这个BitArray:

    BitArray bits = new BitArray(2);
    bits[0] = false;
    bits[1] = true;

Which represents : 10b --> 2 代表: 10b > 2

Let's see what's its value : 让我们看看它的价值是什么:

  int[] array = new int[1];
  bits.CopyTo(array, 0);
  Console.WriteLine(array[0]);  // value=2

Great. 大。

Now I'm changing the first code to : 现在,我将第一个代码更改为:

   bool[] bits = new bool[2] {  false, true }; //same value !
   BitArray myBA4 = new BitArray( bits );

   //and again...
   int[] array = new int[1];
   bits.CopyTo(array, 0);
   Console.WriteLine(array[0]);
  • Exception : Destination array was not long enough. 例外: 目标数组不够长。 Check destIndex and length, and the array's lower bounds. 检查destIndex和length,以及数组的下限。

Question

Where is my mistake ? 我的错误在哪里? I think it should be same result. 我认为应该是相同的结果。

bool[] bits = new bool[2] { false, true };

allocates an array of two elements, and CopyTo is supposed to copy them one by one. 分配两个元素组成的数组, CopyTo应该一个一个地复制它们。 It cannot succeed because 它不能成功,因为

  • the second array is too short; 第二个数组太短;
  • bool cannot be converted to int implicitly. bool不能隐式转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM