Derek Hunziker
Derek Hunziker

Reputation: 13141

ReSharper telling me Boolean.TryParse() inside IF statement redundant?

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?

Upvotes: 3

Views: 912

Answers (1)

ken2k
ken2k

Reputation: 49013

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.

Upvotes: 11

Related Questions