Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between these two comparison statements?

Tags:

c#

.net

Whats the difference between these two comparison statments?

var result = EqualityComparer<T>.Default.Equals(@this, null);
var result = @this == null;

Obviously the aim is to test whether the object '@this' isnull.

like image 248
dotnetnoob Avatar asked Dec 27 '22 17:12

dotnetnoob


1 Answers

Well it depends on the type of @this. If it doesn't have an overload of ==, the second line will just perform a direct reference comparison, whereas the first line will call an overridden Equals method or an implementation of IEquatable.Equals.

Any sensible implementation will give the same result for both comparisons.

like image 104
Jon Skeet Avatar answered Dec 29 '22 11:12

Jon Skeet