Share via


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

(||) 条件OR演算子 bool のはオペランドの論理または実行します。1番目のオペランドが trueと評価されると、2番目のオペランドは評価されません。1番目のオペランドが falseと評価されると、2番目の演算子は、式が true か falseに全体として評価するかどうかを判定します。

解説

x || y

この演算は次の演算に相当します。

x | y

ただし、x が true場合、y または操作が yの値に関係なく true であるため、評価されません。この概念は、「ショートサーキット」評価と呼ばれます。

条件OR演算子オーバーロードできませんが、通常の論理演算子および truefalse 演算子のオーバーロードは、特定の制限と、条件論理演算子のオーバーロードと見なされます。

使用例

次の例では、使用する式は || 1番目のオペランドだけが評価されます。使用する式|両方のオペランドが評価されます。2番目の例では、ランタイム例外が両方のオペランドが評価されると発生します。

class ConditionalOr
{
    // Method1 returns true.
    static bool Method1()
    {
        Console.WriteLine("Method1 called.");
        return true;
    }

    // Method2 returns false.
    static bool Method2()
    {
        Console.WriteLine("Method2 called.");
        return false;
    }


    static bool Divisible(int number, int divisor)
    {
        // If the OR expression uses ||, the division is not attempted
        // when the divisor equals 0.
        return !(divisor == 0 || number % divisor != 0);

        // If the OR expression uses |, the division is attempted when
        // the divisor equals 0, and causes a divide-by-zero exception.
        // Replace the return statement with the following line to
        // see the exception.
        //return !(divisor == 0 | number % divisor != 0);
    }

    static void Main()
    {
        // Example #1 uses Method1 and Method2 to demonstrate 
        // short-circuit evaluation.

        Console.WriteLine("Regular OR:");
        // The | operator evaluates both operands, even though after 
        // Method1 returns true, you know that the OR expression is
        // true.
        Console.WriteLine("Result is {0}.\n", Method1() | Method2());

        Console.WriteLine("Short-circuit OR:");
        // Method2 is not called, because Method1 returns true.
        Console.WriteLine("Result is {0}.\n", Method1() || Method2());


        // In Example #2, method Divisible returns True if the
        // first argument is evenly divisible by the second, and False
        // otherwise. Using the | operator instead of the || operator
        // causes a divide-by-zero exception.

        // The following line displays True, because 42 is evenly 
        // divisible by 7.
        Console.WriteLine("Divisible returns {0}.", Divisible(42, 7));

        // The following line displays False, because 42 is not evenly
        // divisible by 5.
        Console.WriteLine("Divisible returns {0}.", Divisible(42, 5));

        // The following line displays False when method Divisible 
        // uses ||, because you cannot divide by 0.
        // If method Divisible uses | instead of ||, this line
        // causes an exception.
        Console.WriteLine("Divisible returns {0}.", Divisible(42, 0));
    }
}
/*
Output:
Regular OR:
Method1 called.
Method2 called.
Result is True.

Short-circuit OR:
Method1 called.
Result is True.

Divisible returns True.
Divisible returns False.
Divisible returns False.
*/

参照

関連項目

C# 演算子

概念

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

その他の技術情報

C# リファレンス