What is the default boolean value in C#?

前端 未结 6 938
心在旅途
心在旅途 2020-12-05 09:17

A boolean (bool) can\'t be null. And:

bool foo; if(foo){} // Use of unassigned local variable \'foo\'

Why the default value is

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 09:41

    The default value is indeed false.

    However you can't use a local variable is it's not been assigned first.

    You can use the default keyword to verify:

    bool foo = default(bool);
    if (!foo) { Console.WriteLine("Default is false"); }
    

提交回复
热议问题