3

I would like some clarification on the following IF statement. I know this sort of statement can be written in many different ways... that's not what i'm asking. I am really curious why ReSharper 7 is telling me that comparing canceled == true is redundant.

bool canceled;
if (Boolean.TryParse(Request.QueryString["cancel"], out canceled) && 
    canceled == true)
{
    // Transaction canceled...
}

It's my understanding that Boolean.TryParse() will return true/false based on the success of the conversion, not the actual result of the out parameter. Why then would comparing canceled == true be redundant? It very well could be false at that point, right?

Derek Hunziker
  • 12,996
  • 4
  • 57
  • 105

1 Answers1

11

Just use

if (Boolean.TryParse(Request.QueryString["cancel"], out canceled) && canceled)
{
    // Transaction canceled...
}

As canceled is not nullable, you don't need to explicitly compare with the true boolean, as (canceled == true) == canceled.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Ah, thanks... DOH! I just realized ReSharper is not telling me that the entire `canceled == true` condition is redundant... only the `== true`. – Derek Hunziker Feb 26 '13 at 17:47
  • check out http://stackoverflow.com/questions/2977365/which-is-clearer-form-ifvalue-or-ifflag-value/2977394#2977394 – Charles Bretana Feb 26 '13 at 17:50