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?

4

1 回答 1

10

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.

于 2013-02-26T17:46:07.323 回答