loading
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;

class MainClass {
  public static void Main (string[] args) {

    object obj1 = null;
    object obj2 = new object();

    //ReferenceEquals is a static method that takes two parameters, either both of which can be null.  It will not throw a NullReferenceExceptioneither.
    Console.WriteLine(ReferenceEquals(obj1, obj1));
    Console.WriteLine(ReferenceEquals(obj2, obj2));
    Console.WriteLine(ReferenceEquals(obj1, obj2));
    Console.WriteLine(ReferenceEquals(obj2, obj1));
    
    Console.WriteLine();

    //== is an operator, that, in this case (object), behaves identically to ReferenceEquals. It will not throw a NullReferenceExceptioneither.
    Console.WriteLine(obj1 == obj1);
    Console.WriteLine(obj2 == obj2);
    Console.WriteLine(obj1 == obj2);
    Console.WriteLine(obj2 == obj1);

    Console.WriteLine();

    //Equals is an instance method that takes one parameter (which can be null). Since it is an instance method (must be invoked on an actual object), it can't be invoked on a null-reference.

    //obj1 is null. So, it can't be invoked on a null reference.
    // Console.WriteLine(obj1.Equals(obj1);
    Console.WriteLine(obj2.Equals(obj2));
    // Console.WriteLine(obj1.Equals(obj2));
    Console.WriteLine(obj2.Equals(obj1));
  }
}