Advertisement
fcamuso

==null e is null

Apr 17th, 2021
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Nullable_2
  4. {
  5.   class Program
  6.   {
  7.     //differenze tra ==null e is null
  8.     static void Main(string[] args)
  9.     {
  10.       var x = new MyClass();
  11.  
  12.       if (x == null) { Console.WriteLine("Test 1: x is null"); }
  13.      
  14.       if (x != null) { Console.WriteLine("Test 2: x is not null"); }
  15.      
  16.       if (x is null) { Console.WriteLine("Test 3: x is null"); }
  17.      
  18.       if (!(x is null)) { Console.WriteLine("Test 4: x is not null"); }
  19.      
  20.       if (x is object) { Console.WriteLine("Test 5: x is not null"); }
  21.      
  22.       if (!(x is object)) { Console.WriteLine("Test 6: x is null"); }
  23.      
  24.       if (x is { }) { Console.WriteLine("Test 7: x is not null"); }
  25.      
  26.       if (!(x is { })) {  Console.WriteLine("Test 8: x is null");}
  27.      
  28.       //c# 9
  29.       //if (x is not null)
  30.       //  {
  31.       //  Console.WriteLine("Test 9: x is null");
  32.       //}
  33.     }
  34.  
  35. #nullable enable
  36.     static void f(string? s) => Console.WriteLine(s!.Length);
  37.  
  38.   }
  39.  
  40.   public class MyClass
  41.   {
  42.     public static bool operator ==(MyClass left, MyClass right) { return true; }
  43.     public static bool operator !=(MyClass left, MyClass right) { return true; }
  44.   }
  45. }
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement