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
Check if two BitArray objects are equal in C#
The The Following is the syntax for using the
When you assign one The output of the above code is − When two The output of the above code is − To perform true content comparison, you need to create a custom method − The output of the above code is − The BitArray class in C# provides a compact way to store and manipulate arrays of bits. However, checking equality between two BitArray objects requires understanding how the Equals()
BitArray.Equals() method performs reference equality by default, not content comparison. This means it only returns true if both variables point to the same object in memory.Syntax
Equals() method −
bool result = bitArray1.Equals(bitArray2);
Reference Equality vs Content Equality
Using Reference Assignment
BitArray to another, both variables reference the same object in memory −
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);
}
BitArray arr3 = new BitArray(2);
arr3 = arr2;
Console.WriteLine("Is BitArray2 equal to BitArray3? = "+arr2.Equals(arr3));
}
}
Elements in BitArray1...
False
True
Elements in BitArray2...
False
True
Is BitArray2 equal to BitArray3? = True
Using Content Comparison
BitArray objects are created separately, even with identical content, Equals() returns 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);
}
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? = "+arr1.Equals(arr2));
}
}
Elements in BitArray1...
False
True
Elements in BitArray2...
False
True
Is BitArray1 equal to BitArray2? = False
Custom Content-Based Equality Method
using System;
using System.Collections;
public class Demo {
public static bool AreBitArraysEqual(BitArray arr1, BitArray arr2) {
if (arr1.Length != arr2.Length) return false;
for (int i = 0; i
Reference equality: False
Content equality: True
Conclusion
BitArray.Equals() method performs reference equality, not content comparison. For content-based equality, you need to implement a custom comparison method that checks each bit individually. Understanding this distinction is crucial when working with BitArray collections in C#.
