false 运算符(C# 参考)

返回布尔true 以指示操作数为 false,否则返回 false

在 C# 2.0 以前,truefalse 运算符一直用来创建用户定义的可以为 null 的值类型,这些值类型与 SqlBool 等类型兼容。然而,现在该语言提供对可为 null 的值类型的内置支持,并且应该尽可能地使用它们而不是重载 truefalse 运算符。有关更多信息,请参见 可以为 null 的类型(C# 编程指南)

对于可以为 null 的布尔值,表达式 a != b 不一定等同于 !(a == b),因为两个值之一或者全部都有可能为 null。必须单独重载 truefalse 运算符,以便正确处理表达式中的 null 值。下面的示例演示如何重载及使用 truefalse 运算符。

// For example purposes only. Use the built-in nullable bool 
// type (bool?) whenever possible.
public struct DBBool
{
    // The three possible DBBool values.
    public static readonly DBBool Null = new DBBool(0);
    public static readonly DBBool False = new DBBool(-1);
    public static readonly DBBool True = new DBBool(1);
    // Private field that stores –1, 0, 1 for False, Null, True.
    sbyte value;
    // Private instance constructor. The value parameter must be –1, 0, or 1.
    DBBool(int value)
    {
        this.value = (sbyte)value;
    }
    // Properties to examine the value of a DBBool. Return true if this
    // DBBool has the given value, false otherwise.
    public bool IsNull { get { return value == 0; } }
    public bool IsFalse { get { return value < 0; } }
    public bool IsTrue { get { return value > 0; } }
    // Implicit conversion from bool to DBBool. Maps true to DBBool.True and
    // false to DBBool.False.
    public static implicit operator DBBool(bool x)
    {
        return x ? True : False;
    }
    // Explicit conversion from DBBool to bool. Throws an exception if the
    // given DBBool is Null; otherwise returns true or false.
    public static explicit operator bool(DBBool x)
    {
        if (x.value == 0) throw new InvalidOperationException();
        return x.value > 0;
    }
    // Equality operator. Returns Null if either operand is Null; otherwise
    // returns True or False.
    public static DBBool operator ==(DBBool x, DBBool y)
    {
        if (x.value == 0 || y.value == 0) return Null;
        return x.value == y.value ? True : False;
    }
    // Inequality operator. Returns Null if either operand is Null; otherwise
    // returns True or False.
    public static DBBool operator !=(DBBool x, DBBool y)
    {
        if (x.value == 0 || y.value == 0) return Null;
        return x.value != y.value ? True : False;
    }
    // Logical negation operator. Returns True if the operand is False, Null
    // if the operand is Null, or False if the operand is True.
    public static DBBool operator !(DBBool x)
    {
        return new DBBool(-x.value);
    }
    // Logical AND operator. Returns False if either operand is False,
    // Null if either operand is Null, otherwise True.
    public static DBBool operator &(DBBool x, DBBool y)
    {
        return new DBBool(x.value < y.value ? x.value : y.value);
    }
    // Logical OR operator. Returns True if either operand is True, 
    // Null if either operand is Null, otherwise False.
    public static DBBool operator |(DBBool x, DBBool y)
    {
        return new DBBool(x.value > y.value ? x.value : y.value);
    }
    // Definitely true operator. Returns true if the operand is True, false
    // otherwise.
    public static bool operator true(DBBool x)
    {
        return x.value > 0;
    }
    // Definitely false operator. Returns true if the operand is False, false
    // otherwise.
    public static bool operator false(DBBool x)
    {
        return x.value < 0;
    }
    public override bool Equals(object obj)
    {
        if (!(obj is DBBool)) return false;
        return value == ((DBBool)obj).value;
    }
    public override int GetHashCode()
    {
        return value;
    }
    public override string ToString()
    {
        if (value > 0) return "DBBool.True";
        if (value < 0) return "DBBool.False";
        return "DBBool.Null";
    }
}

重载 truefalse 运算符的类型可以用于 ifdowhilefor 语句以及条件表达式中的控制表达式。

如果类型定义了 false 运算符,则它还必须定义 true 运算符。

类型不能直接重载条件逻辑运算符 &&||,但通过重载正则逻辑运算符以及运算符 truefalse 可以达到同样的效果。

C# 语言规范

有关详细信息,请参阅 C# 语言规范。该语言规范是 C# 语法和用法的权威资料。

请参阅

C# 参考

C# 编程指南

C# 关键字

C# 运算符

true(C# 参考)