What is the Default Value of a Boolean in C#?

πŸ’‘ Concept: Default Boolean Value

In C#, the bool data type represents a binary condition: true or false. Its default value is false, unless explicitly initialized.

πŸ“˜ Quick Intro

The default value of a boolean field or array element in C# is false. However, local variables must be assigned before use.

🧠 Analogy

Imagine a light switch. When a new switch is installed in a room and never turned on, it starts off as offβ€”not emitting any light. Similarly, in C#, a bool is false by default, representing an β€œoff” or untriggered state.

πŸ”§ Technical Explanation

  • bool is a value type (System.Boolean) and stores either true or false.
  • Its default value is false when declared as a field, struct member, or array element.
  • Local variables do not get a default valueβ€”you must assign them before using.
  • Static fields and class-level fields are automatically initialized to false.
  • Nullable boolean (bool?) has a default of null, not false.

🎯 Use Cases

  • Use default boolean for flags like isActive, isLoggedIn, etc.
  • Boolean arrays default to all false.
  • Use default values in model validation and condition handling.

πŸ’» C# Code Example

public class User {
    public bool IsVerified;  // default is false
}

public class Program {
    static void Main() {
        User u = new User();
        Console.WriteLine(u.IsVerified);  // Output: false
    }
}

❓ Interview Q&A

Q1: What is the default value of a bool in C#?
A: false

Q2: Do local bool variables have default values?
A: No, they must be initialized before use.

Q3: What is the default value of a bool array element?
A: false

Q4: What happens if a local bool is used uninitialized?
A: It causes a compile-time error.

Q5: What is the default for a bool? nullable bool?
A: null

Q6: Are static boolean fields auto-initialized?
A: Yes, to false

Q7: Can the default value of bool be changed?
A: No, but you can initialize it explicitly.

Q8: How do you explicitly set a bool to true?
A: Use assignment: flag = true;

Q9: Is bool a reference or value type?
A: It's a value type.

Q10: Can bool be null in C#?
A: Only if declared as nullable (i.e., bool?).

πŸ“ MCQs

Q1. What is the default value of a bool in C#?

  • true
  • false
  • null
  • 0

Q2. Do local variables of type bool have default values?

  • Yes
  • No
  • Sometimes
  • Only in structs

Q3. What is the default of a bool[] array?

  • true
  • null
  • false
  • undefined

Q4. What is the default of a nullable bool?

  • false
  • true
  • null
  • undefined

Q5. What will Console.WriteLine(new bool()); print?

  • True
  • False
  • Null
  • Error

Q6. Can you assign null to a bool?

  • Yes
  • No
  • Only in arrays
  • Only at runtime

Q7. Which type does bool derive from?

  • System.Object
  • System.ReferenceType
  • System.ValueType
  • System.Primitive

Q8. Can static bool fields be uninitialized?

  • Yes, default is true
  • Yes, default is false
  • No
  • Only in classes

Q9. How to explicitly assign true to a bool?

  • flag := true;
  • flag = 1;
  • flag = true;
  • flag.set(true);

Q10. What is the size of bool in C#?

  • 1 byte
  • 4 bytes
  • 2 bits
  • 64 bits

πŸ’‘ Bonus Insight

Boolean default values are foundational for building logic around system states. Understanding when and how they are initialized prevents unnecessary bugs in class-level declarations or array manipulations.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ”œ Next Topic

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: