Operators for nullable type

 
using System;
using System.IO;

class Test
{
    static void Main()
    {
        int? x = 5;
        int? y = null;

        // Equality operator examples 
        Console.WriteLine(x == y);
        // False 
        Console.WriteLine(x == null); // False 
        Console.WriteLine(x == 5);  // True 
        Console.WriteLine(y == null); // True 
        Console.WriteLine(y == 5);  // False 
        Console.WriteLine(y != 5);  // True

        // Relational operator examples 
        Console.WriteLine(x < 6);  // True 
        Console.WriteLine(y < 6);  // False 
        Console.WriteLine(y > 6);  // False

        // All other operator examples
        Console.WriteLine(x + 5);  // 10
        Console.WriteLine(x + y);  // null (prints empty line)

    }
}

The output:


False
False
True
True
False
True
True
False
False
10
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.