หากต้องการตรวจสอบว่า BitArray เป็นแบบอ่านอย่างเดียวหรือไม่ รหัสจะเป็นดังนี้ -
ตัวอย่าง
using System; using System.Collections; public class Demo { public static void Main() { BitArray arr1 = new BitArray(2); BitArray arr2 = new BitArray(2); arr1[0] = false; arr1[1] = true; Console.WriteLine("Elements in BitArray1..."); foreach (bool res in arr1) { Console.WriteLine(res); } arr2[0] = false; arr2[1] = true; Console.WriteLine("Elements in BitArray2..."); foreach (bool res in arr2) { Console.WriteLine(res); } Console.WriteLine("Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1)); Console.WriteLine("Is BitArray synchronized? = "+arr2.IsSynchronized); Console.WriteLine("Is BitArray read-only? = "+arr2.IsReadOnly); } }
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in BitArray1... False True Elements in BitArray2... False True Is BitArray1 equal to BitArray2? = False Is BitArray synchronized? = False Is BitArray read-only? = False
ตัวอย่าง
เรามาดูตัวอย่างกัน −
using System; using System.Collections; public class Demo { public static void Main() { BitArray arr1 = new BitArray(2); BitArray arr2 = new BitArray(2); arr1[0] = false; arr1[1] = true; Console.WriteLine("Elements in BitArray1..."); foreach (bool res in arr1) { Console.WriteLine(res); } Console.WriteLine("Is BitArray1 read-only? = "+arr1.IsReadOnly); arr2[0] = true; arr2[1] = false; Console.WriteLine("Elements in BitArray2..."); foreach (bool res in arr2) { Console.WriteLine(res); } Console.WriteLine("Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1)); Console.WriteLine("Is BitArray2 read-only? = "+arr2.IsReadOnly); } }
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in BitArray1... False True Is BitArray1 read-only? = False Elements in BitArray2... True False Is BitArray1 equal to BitArray2? = False Is BitArray2 read-only? = False