BitArray dùng khi cần lưu trữ bit nhưng không biết trước số lượng bit.
Một số thuộc tính:
| STT | Thuộc tính & Mô tả |
|---|---|
| 1 | Count – Số phần tử trong BitArray. |
| 2 | IsReadOnly – Chỉ đọc hay không. |
| 3 | Item – Lấy/đặt giá trị bit tại vị trí cụ thể. |
| 4 | Length – Lấy/đặt số phần tử trong BitArray. |
Ví dụ dùng IsReadOnly:
Ví dụ
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
BitArray ba1 = new BitArray(5);
BitArray ba2 = new BitArray(5);
byte[] a = { 90 };
byte[] b = { 30 };
ba1 = new BitArray(a);
ba2 = new BitArray(b);
Console.WriteLine("Bit array ba1: 60");
for (int i = 0; i < ba1.Count; i++) {
Console.Write("{0, -4} ", ba1[i]);
}
Console.WriteLine();
Console.WriteLine("IsReadOnly = " + ba1.IsReadOnly);
Console.WriteLine("IsReadOnly = " + ba2.IsReadOnly);
Console.ReadKey();
}
}
}