TheDeveloperBlog.com


C# Bool Array Memory

Bool array. A bool array requires one byte per element. It can store many true and false values in your C# program. Bool arrays are often ideal for this purpose. They are simple and allow for safe and clear code.

Bool

Example. To start, we allocate and initialize bool arrays in the C# language. The syntax is the same as other arrays, in that you need to specify the array length inside the square brackets.

C# program that uses bool array

using System;

class Program
{
    static void Main()
    {
	//
	// Initialize new bool array.
	//
	bool[] array = new bool[10];
	array[0] = false; // <-- Not needed
	array[1] = true;
	array[2] = false; // <-- Not needed
	array[3] = true;
	//
	// Loop through all values.
	//
	foreach (bool value in array)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

False
True
False
True
False
False
False
False
False
False

When you allocate the bool array, its values are filled with False. You do not need to initialize any values to False in your code. Instead, you should initialize values to True as required.

Tip: You can loop over bool arrays with foreach, as shown above, or with for, as shown further on.


Sort. You can quickly sort boolean arrays in the C# language. Sorting bool arrays is extremely useful for certain algorithms, such as those that favor some object, but do not want to remove any less desirable elements.

Bool Sort

Memory. The bool value can be expressed with a single bit in memory—one means true and zero means false. However, the bool type in C# is expressed in eight bits, or one byte. It is considerably less efficient than some other approaches.

And: To prove this, we look at a program that allocates one million bools. This results in 1,000,012 bytes being allocated.

However: One million bits is only 125,000 bytes. So a bool array representation is less efficient.

Google Search
C# program that allocates bool array

using System;

class Program
{
    static void Main()
    {
	//
	// Initialize new bool array.
	//
	bool[] array = new bool[1000000];
	//
	// Initialize each other value to true.
	//
	for (int i = 0; i < array.Length; i += 2)
	{
	    array[i] = true;
	}
    }
}

Notes

Size of the array in memory: 1,000,012 bytes [CLRProfiler].
Each bool requires 1 byte.
Array reference requires 12 bytes.
Platform is x86.


Arguments. As with other arrays, you can pass bool arrays as arguments by using the bool[] type for the parameter. You can also return bool arrays from methods with the same type. The square brackets are part of the type.

Tip: Due to the extra address space required for booleans, it is better to use BitArray when you have trillions of values.


BitArray. There is a BitArray class in the System.Collections namespace. This is more compact, as noted on the MSDN page. This site has an in-depth look at bit arrays as implemented by the BitArray class.

BitArray Collection

Summary. We looked at bool arrays in the C# language, first running an example program. The program showed that elements in bool arrays are initialized to false automatically. We saw how to loop through bool arrays.

Finally: We saw that bool arrays require one byte for each element, which is eight times the size that could represent the values.