相关疑难解决方法(0)

为什么可空的bool不允许if(可为空)但允许if(nullable == true)?

此代码编译:

private static void Main(string[] args)
{
    bool? fred = true;

    if (fred == true)
        Console.WriteLine("fred is true");
    else if (fred == false)
         Console.WriteLine("fred is false");
    else Console.WriteLine("fred is null");
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译.

private static void Main(string[] args)
{
    bool? fred = true;

    if (fred)
        Console.WriteLine("fred is true");
    else if (!fred)
         Console.WriteLine("fred is false");
    else Console.WriteLine("fred is null");
}
Run Code Online (Sandbox Code Playgroud)

我想如果(booleanExpression == true)应该是冗余.在这种情况下为什么不呢?

.net c# nullable

36
推荐指数
2
解决办法
6001
查看次数

标签 统计

.net ×1

c# ×1

nullable ×1