Why doesn't this static bool need to be initialized?

if you call checkBool, it will always return “why this doesn't work”

Why is this and why you do not need to initialize _bool?

public sealed class falsefalse
{
    private static bool _bool;
    public static string checkBool()
    {
        if (!_bool)
            return "why does this not fail";
        else return "";
    }
}
+4
source share
2 answers

Class fields have default values unless you explicitly initialize them. The default value for the type boolis false. See Specification C # 10.4.4 Field Initialization :

The initial value of the field, whether it is a static field or an instance field, is the default value (section 5.2) of the type field.

Take a look at the table of default values ​​(link to C #)

+5

. default(bool) - false, - - _bool false.

+1

Source: https://habr.com/ru/post/1544759/


All Articles