Reputation:
I have a query in checking condition.
var hasListHeader = false;
using(SqlDataReader reader = sql.ExecuteQueryReturnDR(query, sqlParams)) //this will return SqlDataReader
{
if(hasListHeader = reader.HasRows)
{
}
}
reader.HasRows
and then checking condition?Instead of doing like this, am checking like above. Is that good practice?
hasListHeader = reader.HasRows;
if(hasListHeader)
{
}
Upvotes: 0
Views: 176
Reputation: 144206
Yes you can check boolean like that since assignment is an expression which has the value of the right-hand operand, so
hasListHeader = reader.HasRows
evalulates to the value of reader.HasRows
and assigns that value to hasListHeader
as a side-effect.
It's not considered good style to do this however, and moving the assignment above the if
statement is considered clearer.
Upvotes: 0
Reputation: 29186
You should use
hasListHeader = reader.HasRows;
if(hasListHeader)
{
}
only if you intend to use the variable hasListHeader
for later use, perhaps in the same method or elsewhere in the codebase.
If you don't need hasListHeader
after the if
statement, then I would remove it and use
if(reader.HasRows)
{
}
Upvotes: 3