Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the Count property of BitArray class in C#?
The Count property of the BitArray class in C# returns the total number of elements (bits) in the BitArray. This is a read-only property that gives you the size of the BitArray, which is determined when the BitArray is created.
Syntax
Following is the syntax for accessing the Count property −
int count = bitArray.Count;
Return Value
The Count property returns an int value representing the number of bits in the BitArray.
Using Count Property with BitArray
Let us first create a BitArray and then use the Count property to get the number of elements −
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr = new BitArray(10);
Console.WriteLine("Count: {0}", arr.Count);
}
}
The output of the above code is −
Count: 10
Count Property with Different BitArray Sizes
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr1 = new BitArray(5);
BitArray arr2 = new BitArray(new bool[] {true, false, true});
BitArray arr3 = new BitArray(20);
Console.WriteLine("BitArray 1 Count: {0}", arr1.Count);
Console.WriteLine("BitArray 2 Count: {0}", arr2.Count);
Console.WriteLine("BitArray 3 Count: {0}", arr3.Count);
}
}
The output of the above code is −
BitArray 1 Count: 5 BitArray 2 Count: 3 BitArray 3 Count: 20
Count vs Length Property
The BitArray class has both Count and Length properties, and they return the same value −
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr = new BitArray(15);
Console.WriteLine("Count: {0}", arr.Count);
Console.WriteLine("Length: {0}", arr.Length);
Console.WriteLine("Are they equal? {0}", arr.Count == arr.Length);
}
}
The output of the above code is −
Count: 15 Length: 15 Are they equal? True
Conclusion
The Count property of BitArray class provides a simple way to determine the total number of bits in a BitArray. It returns the same value as the Length property and reflects the size specified during BitArray creation.
