Created
September 11, 2018 18:23
-
-
Save lomholdt/4bec7f038b2e2a1592c64a7d037ff98d to your computer and use it in GitHub Desktop.
Overriden == operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var person1 = new Person("Jonas", "Lomholdt"); | |
var personN = new Person("Jonas", "Lomholdt"); | |
var person2 = new Person("Mike", "Tyson"); | |
Person person3 = null; | |
// Show that comparing instances is working | |
var expected = true; | |
var actual = Person1 == PersonN; // true | |
var expected = false; | |
var actual = Person1 == Person2; // False | |
var expected = true; | |
var actual = Person1 != Person2; // true | |
var expected = true; | |
var actual = Person3 is null; // true | |
var expected = true; | |
var actual = Person3 == null; // true | |
// Here comes the fun part | |
var expected = false; | |
var actual = Person1 == null; // true | |
// What?! This should be false. Person1 is instantiated | |
// Using the is operator however works just fine every time. | |
var expected = false; | |
var actual = Person1 is null; // false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment