false 연산자(C# 참조)

업데이트: 2008년 7월

피연산자가 false임을 나타낼 경우 bool 값 true를 반환하고, 그렇지 않을 경우 false를 반환합니다.

C# 2.0 이전 버전에서는 true 및 false 연산자를 사용해서 SqlBool과 같은 형식과 호환되는 사용자 정의 null 허용 값 형식을 만들었습니다. 그러나 이 버전에서는 null 허용 값 형식에 대한 지원이 기본적으로 제공되므로 가능하면 true 및 false 연산자를 오버로드하는 대신 null 허용 값 형식을 사용해야 합니다. 자세한 내용은 nullable 형식(C# 프로그래밍 가이드)을 참조하십시오.

null 허용 부울을 사용하면 a != b 식은 두 값 중 하나 이상이 null일 수 있으므로 !(a == b)과 반드시 일치하지는 않습니다. 식의 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 연산자를 오버로드하는 형식은 if, do, whilefor 문과 조건식에서 제어식에 사용할 수 있습니다.

false 연산자를 정의하는 형식은 true 연산자도 정의해야 합니다.

한 형식으로 조건부 논리 연산자(&&||)를 직접 오버로드할 수 없지만, 일반적인 논리 연산자와 true 및 false 연산자를 오버로드하면 조건부 논리 연산자를 오버로드한 것과 같은 결과를 얻을 수 있습니다.

C# 언어 사양

자세한 내용은 C# 언어 사양의 다음 단원을 참조하십시오.

  • 10.10.1 단항 연산자

  • 7.11.2 사용자 정의 조건부 논리 연산자

  • 7.19 부울 식

참고 항목

개념

C# 프로그래밍 가이드

참조

C# 키워드

C# 연산자

true(C# 참조)

기타 리소스

C# 참조

변경 기록

날짜

변경 내용

이유

2008년 7월

true 및 false 연산자를 오버로드하는 방법에 대한 정보가 추가되었습니다.

향상된 기능 관련 정보