bool? with & and | Operators - CSharp Language Basics

CSharp examples for Language Basics:Nullable

Introduction

When supplied operands of type bool? the & and | operators treat null as an unknown value.

null | true is true, because:

If the unknown value is false, the result would be true.

If the unknown value is true, the result would be true.

null & false is false.

The following example enumerates other combinations:

Demo Code

using System;// w ww. j  a  v  a  2s.  c om
class Test
{
   static void Main()
   {
      bool? n = null;
      bool? f = false;
      bool? t = true;
      Console.WriteLine (n | n);    // (null)
      Console.WriteLine (n | f);    // (null)
      Console.WriteLine (n | t);    // True
      Console.WriteLine (n & n);    // (null)
      Console.WriteLine (n & f);    // False
      Console.WriteLine (n & t);    // (null)
   }
}

Result


Related Tutorials