Bool Array in C#


In a bool array, you can store true and false values. To set a bool array, use the new operator −

bool[] arr = new bool[5];

To add elements in the array −

arr[0] = true;
arr[1] = true;
arr[2] = false;
arr[3] = true;
arr[4] = true;

Let us see the complete code −

Example

 Live Demo

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("Displaying values...");

      foreach (bool res in arr) {
         Console.WriteLine(res);
      }
   }
}

Output

Displaying values...
True
True
False
True
True

Updated on: 22-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements