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

Lớp BitArray trong C# là gì?

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:

STTThuộc tính & Mô tả
1Count – Số phần tử trong BitArray.
2IsReadOnly – Chỉ đọc hay không.
3Item – Lấy/đặt giá trị bit tại vị trí cụ thể.
4Length – 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();
        }
    }
}