Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra BitArray có chỉ đọc (read-only) trong C#

Giới thiệu

Thuộc tính IsReadOnly của BitArray luôn trả về false vì BitArray cho phép sửa đổi bit qua indexer hoặc phương thức Set(), SetAll().

Ví dụ 1: Kiểm tra IsReadOnly và IsSynchronized

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);
    }
}

Kết quả

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

Ví dụ 2: Kiểm tra IsReadOnly trên hai BitArray khác nhau

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);
    }
}

Kết quả

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

Đặc điểm BitArray

Thuộc tính Giá trị Giải thích
IsReadOnly false Luôn có thể sửa đổi bit
IsSynchronized false Không an toàn luồng
IsFixedSize true* Length có thể thay đổi nhưng phức tạp

Cách sửa đổi BitArray

BitArray bits = new BitArray(8);

// Indexer (get/set)
bits[0] = true;
bool b = bits[0];

// Phương thức Set
bits.Set(1, true);
bits.SetAll(true);  // Đặt tất cả = true
bits.SetAll(false); // Đặt tất cả = false

// Phép bitwise (trả về IEnumerable, không sửa gốc)
var result = bits.And(otherBits);
var result2 = bits.Or(otherBits);
var result3 = bits.Xor(otherBits);
var inverted = bits.Not(); // Trả về BitArray mới

Lưu ý

  • Equals() so sánh THAM CHIẾU, không so sánh nội dung
  • Để so sánh nội dung: bits.Cast<bool>().SequenceEqual(other.Cast<bool>())
  • BitArray không generic, lưu bool nhưng compact (1 bit/phần tử)