要将BitArray元素复制到数组,代码如下-
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); } bool[] boolArr = new bool[2]; boolArr[0] = true; boolArr[1] = false; arr.CopyTo(boolArr, 0); Console.WriteLine("\nArray..."); foreach(Object obj in boolArr){ Console.WriteLine(obj); } } }
输出结果
这将产生以下输出-
Elements in BitArray... False True Array... False True
现在让我们来看另一个示例-
using System; using System.Collections; public class Demo { public static void Main(){ BitArray arr = new BitArray(5); arr[0] = false; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true; Console.WriteLine("Elements in BitArray..."); foreach (bool res in arr){ Console.WriteLine(res); } bool[] boolArr = new bool[10]; boolArr[0] = true; boolArr[1] = false; arr.CopyTo(boolArr, 0); Console.WriteLine("\nArray..."); foreach(Object obj in boolArr){ Console.WriteLine(obj); } } }
输出结果
这将产生以下输出-
Elements in BitArray... False True False True True Array... False True False True True False False False False False