true 演算子 (C# リファレンス)

オペランドが true であることを示す場合は bool 値 true を返し、それ以外の場合は false を返します。

C# 2.0 より前では、SqlBool などの型と互換性のあるユーザー定義の null 許容値型を作成するのに true 演算子と false 演算子が使用されていました。 しかし、現在、null 許容値型の組み込みサポートが追加され、true 演算子と false 演算子をオーバーロードすることなく、いつでも null 許容値型を使用できるようになりました。 詳細については、「null 許容型 (C# プログラミング ガイド)」を参照してください。

null 許容ブール型では、式 a != b と !(a == b) は必ずしも同じにはなりません。一方または両方の値が null になる可能性があるためです。 式内の null 値を正しく識別するには個別に true 演算子と false 演算子の両方をオーバーロードする必要があります。 true 演算子と false 演算子をオーバーロードし、使用する方法を次の例に示します。

// 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";
    }
}

true 演算子と false 演算子をオーバーロードする型は ifdowhile、および for の各ステートメント内の制御式や、条件式で使用できます。

型で true 演算子を定義する場合は、false (C# リファレンス) 演算子も定義する必要があります。

型は条件論理演算子 (&amp;&amp; 演算子 (C# リファレンス) および|| 演算子 (C# リファレンス)) を直接オーバーロードすることはできません。条件論理演算子を直接オーバーロードする場合と同様の効果を得るには、通常の論理演算子と true および false の演算子をオーバーロードします。

C# 言語仕様

詳細については、「C# 言語仕様」を参照してください。 言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

参照

参照

C# のキーワード

C# 演算子

false (C# リファレンス)

概念

C# プログラミング ガイド

その他の技術情報

C# リファレンス