Is there a shorthand for the ternary operator in C#? -
background
in php there shorthand ternary operator:
$value = ""; echo $value ?: "value empty"; // same $value == "" ? "value empty" : $value;
in js there's equivalent:
var value = ""; var ret = value || "value empty"; // same var ret = value == "" ? "value empty" : value;
but in c#, there's (as far know) "full" version works:
string value = ""; string result = value == string.empty ? "value empty" : value;
so question is: there shorthand ternary operator in c#, , if not, there workaround?
research
i found following questions, they're referring use ternary operator shorthand if-else:
benefits of using conditional ?: (ternary) operator
and one, it's concerning java:
is there php short version of ternary operator in java?
what have tried
use shorthand style of php (failed due syntax error)
string value = ""; string result = value ?: "value empty";
use shorthand style of js (failed because "the ||
operator not applicable string
, string
.")
string value = ""; string result = value || "value empty";
there no shorthand when string empty. there shorthand when string null
:
string value = null; string result = value ?? "value null";
Comments
Post a Comment