I was reviewing some code, and I found something that looked like this:
public class MyClass
{
public bool IsEditable { get; set; }
public void HandleInput()
{
if (IsEditable.Equals(false))
{
//do stuff
}
}
}
As far as I know, (IsEditable.Equals(false)) is identical to (IsEditable == false) (and also the same as (!IsEditable)).
Besides personal preference, is there any difference at all between .Equals() and ==, specifically when used to compare bools?
The
Equalsway appears to be significantly slower – roughly 2.7 times in debug mode, and more than seven times in release mode.Here is my quick and dirty benchmark:
Running this produces the following results:
In debug mode
In release mode
Equalsappears to be the slowest. There appears to be little difference between==and!=. However,if (!boolExpr)appears to be the clear winner.