difference between this two condition
what is difference in this two condition
return Convert.ToInt32(cmd.ExecuteScalar()) == 1 ? true : f开发者_运维问答alse;
return res =! 0 ? true:false;
return Convert.ToInt32(cmd.ExecuteScalar()) > 0 ? true : false;
what is difference in this three condition when i check the existence of information in the table [mysql]
You are using ternary operator.
1st->return Convert.ToInt32(cmd.ExecuteScalar()) == 1 ? true : false;
True:-When the output of Convert.ToInt32(cmd.ExecuteScalar())
is equal to 1.
False:-if its not equal to 1.
2nd->return res =! 0 ? true:false;
it have syntax error
The correct one is that return res != 0 ? true:false;
it have syntax error
True:-when res not equal to 0.
False:-if res equal to 0.
3rd->return Convert.ToInt32(cmd.ExecuteScalar()) > 0 ? true : false;
true:-if value of Convert.ToInt32(cmd.ExecuteScalar())
is greater than 0.
otherwise false.
You don't need to specify ? true : false
in any of these. Just return Convert.ToInt32(cmd.ExecuteScalar()) == 1;
(for example) will do the trick. There will be no difference as far as the database is concerned and any performance difference in the application will be negligable and should be ignored. What's left is just the basic logical differences between comparing the result as equal to 1, not equal to 0 or greater than zero.
Well, the first and third expressions are similar. But the first returns true only if the result is 1, the third returns true as long as the result isn't zero. So if the sql command returns 2 for example, the result will be different.
Expressions number two, I'm not sure why you want to compare it with the other two.
BTW, the ternary operator ?: is redundant in all three lines. You could just as well write
return Convert.ToInt32(cmd.ExecuteScalar()) == 1;
return res =! 0;
return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
All three versions have slightly different logic:
- The first version returns
true
when the value is1
, otherwisefalse
. - The second version returns
false
when the value is0
, otherwisetrue
. - The third version returns
true
when the value is positive, otherwisefalse
.
If your value can only ever be 0
or 1
then all three of them will be functionally equivalent, but this isn't guaranteed by the code itself since an Int32
has 2^32 different states.
精彩评论