Is there a way to short circuit a triple operation?

I can write

if(Model.DecisionReason != null && Model.DecisionReason.Length > 35)
    return Model.DecisionReason.Substring(0, 32) + "...";
else
    return Model.DecisionReason;

and comparison &&in ifwill be a short circuit, preventing an exception if Model.DecisionReasonnull. However, if I write

return (Model.DecisionReason != null && Model.DecisionReason.Length > 35) ?
     Model.DecisionReason.Substring(0, 32) + "..." :
     Model.DecisionReason;

There is no short circuit, and I got the exception. Is there a way to do this for a short circuit, or am I forced to either wrap the length comparison in the test iffor tees or sockets (not the most read)?

+3
source share
1 answer

Both code samples that you wrote will have the same behavior. This is not ifthat short circuit, but simply the main component of the expression itself &&.

+7
source

All Articles