a ? b : c?
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
x = a ? b : c;
is the shorthand version of
if (a) { x = b; } else { x = c; }
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
It's known as a ternary operator. Basically, this is an operator that reads like this: "If the condition is true then use the value immediately after the question mark, otherwise use the value after the colon". Another operator you may sometimes see is the ?? operator. This is known as the coalescing null operator, and is used in statements like this:
MyClass a = value1 ?? value2 ?? new MyClass();
This reads like this "MyClass a is assigned value1, unless value1 is null in which case, it will use value2, unless value2 is null in which cass it uses new MyClass();" This is an efficient way to assign a default value if a null value is encountered.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
x = a ? b : c;
is the shorthand version of
if (a) { x = b; } else { x = c; }
Except that ?: counts as an expression, instead of a statement. So there are more places where you're allowed to use it. Such as:
if (a?b:c) { /* some code */ }
or
int x = a?b:c;
(that doesn't mean you're wrong, I just think it might help bfis108137 to know that)
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.
It's called a ternary operation, and is the shortcut of doing this:
int z = 0;
int x = 1;
int y = 2;
if (x==y)
{
z = x;
}
else
{
z = y;
}// in the format you cited:
z = (x ==y) ? x : y;
Many high-browed programmers don't like it, but as long as you keep it to just one comparison instead of stacking them, I think it's an acceptable coding practice.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
It's called a ternary operation, and is the shortcut of doing this:
int z = 0;
int x = 1;
int y = 2;
if (x==y)
{
z = x;
}
else
{
z = y;
}// in the format you cited:
z = (x ==y) ? x : y;
Many high-browed programmers don't like it, but as long as you keep it to just one comparison instead of stacking them, I think it's an acceptable coding practice.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Right, it should not be abused; I would not use it as
z = (x ==y) ? x : y;
, because an if statement would work for that. One situation I use it in is something like this:string.Format
(
"{0} record{1} processed."
,
count
,
count==1?"":"s"
) ;I find this more readable than the alternatives. Whenever I see an application write something like, "1 records processed", I :sigh: . Come on, guys, use the ternary operator!
-
What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.