Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assuming this != null when implementing IComparable<T>

Tags:

c#

icomparable

I have an object of type T which implements IComparable<T>. Is it okay when implementing bool Equals (T obj) to ommit the check if (ReferenceEquals(this, null)) { DoSomething() }? Can I assume that since the function could be called this is already not null?

Thank you very much.

like image 616
Miguel Avatar asked Mar 09 '11 10:03

Miguel


2 Answers

Yes you can assume that if the function has been called on an object, then that object is not null.

like image 148
Nick Avatar answered Sep 28 '22 06:09

Nick


You should always assume this != null, because C# guarantees it.

like image 42
Gabe Avatar answered Sep 28 '22 05:09

Gabe