簡體   English   中英

BitArray無法正常工作

[英]BitArray doesn't work as expected

我有這個BitArray:

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

代表: 10b > 2

讓我們看看它的價值是什么:

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

大。

現在,我將第一個代碼更改為:

   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]);
  • 例外: 目標數組不夠長。 檢查destIndex和length,以及數組的下限。

我的錯誤在哪里? 我認為應該是相同的結果。

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

分配兩個元素組成的數組, CopyTo應該一個一個地復制它們。 它不能成功,因為

  • 第二個數組太短;
  • bool不能隱式轉換為int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM