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
Bool Array in C#
A bool array in C# is used to store multiple boolean values (true and false) in a single collection. Boolean arrays are useful for tracking states, flags, or conditions across multiple elements.
Syntax
Following are the different ways to declare and initialize a bool array −
// Declaration with size
bool[] arrayName = new bool[size];
// Declaration with initialization
bool[] arrayName = {true, false, true};
// Using new keyword with values
bool[] arrayName = new bool[] {true, false, true};
Creating and Initializing Bool Arrays
Method 1: Declaration with Size and Assignment
You can create a bool array with a specific size and assign values to individual elements −
using System;
public class Demo {
public static void Main() {
bool[] arr = new bool[5];
arr[0] = true;
arr[1] = true;
arr[2] = false;
arr[3] = true;
arr[4] = true;
Console.WriteLine("Bool array values:");
foreach (bool value in arr) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
Bool array values: True True False True True
Method 2: Direct Initialization
You can initialize a bool array with values at the time of declaration −
using System;
public class BoolArrayExample {
public static void Main() {
bool[] permissions = {true, false, true, false, true};
Console.WriteLine("Permission settings:");
for (int i = 0; i < permissions.Length; i++) {
Console.WriteLine($"Permission {i + 1}: {permissions[i]}");
}
}
}
The output of the above code is −
Permission settings: Permission 1: True Permission 2: False Permission 3: True Permission 4: False Permission 5: True
Default Values
When you create a bool array without explicit initialization, all elements are automatically set to false by default −
using System;
public class DefaultValues {
public static void Main() {
bool[] flags = new bool[4];
Console.WriteLine("Default bool array values:");
for (int i = 0; i < flags.Length; i++) {
Console.WriteLine($"Index {i}: {flags[i]}");
}
}
}
The output of the above code is −
Default bool array values: Index 0: False Index 1: False Index 2: False Index 3: False
Common Use Cases
Bool arrays are commonly used for tracking multiple boolean states, such as user preferences, feature toggles, or validation results −
using System;
public class BoolArrayUseCases {
public static void Main() {
// Feature toggles
bool[] features = {true, false, true, true};
string[] featureNames = {"Dark Mode", "Notifications", "Auto-Save", "Cloud Sync"};
Console.WriteLine("Feature Status:");
for (int i = 0; i < features.Length; i++) {
string status = features[i] ? "Enabled" : "Disabled";
Console.WriteLine($"{featureNames[i]}: {status}");
}
// Count enabled features
int enabledCount = 0;
foreach (bool feature in features) {
if (feature) enabledCount++;
}
Console.WriteLine($"\nTotal enabled features: {enabledCount}");
}
}
The output of the above code is −
Feature Status: Dark Mode: Enabled Notifications: Disabled Auto-Save: Enabled Cloud Sync: Enabled Total enabled features: 3
Conclusion
Bool arrays in C# provide an efficient way to store and manage multiple boolean values. They default to false when uninitialized and are particularly useful for tracking states, permissions, or feature flags in applications.
