Skip to content

Instantly share code, notes, and snippets.

@lomholdt
Created September 11, 2018 18:23
Show Gist options
  • Save lomholdt/4bec7f038b2e2a1592c64a7d037ff98d to your computer and use it in GitHub Desktop.
Save lomholdt/4bec7f038b2e2a1592c64a7d037ff98d to your computer and use it in GitHub Desktop.
Overriden == operator
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