迭代C#中的BitArray的枚举数
以下是使用Enumerator遍历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] = true; arr1[2] = false; arr1[3] = true; arr1[4] = true; Console.WriteLine("Enumerator that iterates through BitArray1"); IEnumerable demoEnum = arr1; foreach(Object ob in demoEnum){ Console.WriteLine(ob); } arr2[0] = true; arr2[1] = true; arr2[2] = true; arr2[3] = false; arr2[4] = true; Console.WriteLine("Enumerator that iterates through BitArray2"); demoEnum = arr2; foreach(Object ob in demoEnum){ Console.WriteLine(ob); } Console.WriteLine("Is BitArray1 equal to BitArray2? = "+arr1.Equals(arr2)); } }
输出结果
这将产生以下输出-
Enumerator that iterates through BitArray1 False True False True True Enumerator that iterates through BitArray2 True True True False True Is BitArray1 equal to BitArray2? = False
示例
现在让我们来看另一个示例-
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("Enumerator that iterates through BitArray"); IEnumerable demoEnum = arr; foreach(Object ob in demoEnum){ Console.WriteLine(ob); } } }
输出结果
这将产生以下输出-
Enumerator that iterates through BitArray False True False True True