C# 中 BitArray 包含的元素数量?

csharpserver side programmingprogramming更新于 2025/8/9 13:37:17

获取 BitArray 包含的元素数量,代码如下 −

示例

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr1 = new BitArray(5);
      BitArray arr2 = new BitArray(5);
      arr1[0] = false;
      arr1[1] = false;
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("BitArray1 elements...");
      foreach (bool res in arr1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("
BitArray2 elements...");       foreach (bool res in arr2) {          Console.WriteLine(res);       }       Console.WriteLine("
Bitwise OR operation...");       IEnumerable demoEnum = arr1.Or(arr2);       foreach(Object ob in demoEnum) {          Console.WriteLine(ob);       }       Console.WriteLine("
Number of elements in the resultant BitArray = "+arr1.Count);    } }

输出

这将产生以下输出 −

BitArray1 elements...
False
False
False
False
False
BitArray2 elements...
False
True
False
False
False
Bitwise OR operation...
False
True
False
False
False
Number of elements in the resultany BitArray = 5

示例

现在让我们看另一个例子 −

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr = new BitArray(2);
      arr[0] = false;
      arr[1] = true;
      Console.WriteLine("Elements in BitArray...");
      foreach (bool res in arr) {
         Console.WriteLine(res);
      }
      Console.WriteLine("
Number of elements in the BitArray = "+arr.Count);       bool[] boolArr = new bool[2];       boolArr[0] = true;       boolArr[1] = false;       arr.CopyTo(boolArr, 0);       Console.WriteLine("
Array...");       foreach(Object obj in boolArr) {          Console.WriteLine(obj);       }    } }

输出

这将产生以下输出 −

Elements in BitArray...
False
True

Number of elements in the BitArray = 2
Array...
False
True

相关文章