Applying the ternary operator ?: for an expression that can only evaluate to true/false (x==y
) is just downright redundant (it's just downright redundant [it's redundant]).
The above sentence might be easier to read for someone who doesn't understand English very well as they'll know what to look up first in the dictionary and, if spoken, due to the repetition. Yet for native English speakers, the sentence is awkward and, well, redundant.
In your example, either the operator is being used for documentation purposes, or, without meaning to offend, I suspect that there's a poor understanding of how operators and expressions work.
Whether it's a lack of understanding or some bizarre attempt at documentation, we can do this without redundant operators like this:
var isMale = (row["Gender"].ToString() == "M"); // bool
or...
var isMale = (row["Gender"].ToString() == "M"); // true/false
... or better yet, explicitly specify the appropriate type for 'isMale' instead of relying on implicit typing:
bool isMale = (row["Gender"].ToString() == "M");
I've also seen people pessimize their code this way (or using if/else):
bool something = some_int ? true: false;
There's no need to do this and, while the compiler may optimize this, it is inherently less efficient to rely on branching mechanisms over something as simple as this:
bool something = some_int != 0;
... which has the same effect but without the roundabout process of using conditional branching.
This kind of code is just embarrassing. It's like seeing:
switch (x)
{
case 1:
y = 1;
break;
case 2:
y = 2;
break;
case 3:
y = 3;
break;
// etc. for all possible values of x
}
The above would most certainly be considered uber-n00b code by most, yet it is not any less redundant logically than x == y ? true: false
or x ? true: false
(as opposed to x != 0
).