ต่อไปนี้คือโค้ดที่วนซ้ำผ่าน BitArray พร้อม Enumerator -
ตัวอย่าง
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