I would avoid using such statement. It doesn't seem to be a right place for this construction inside ?:
operators and I do not see major benefits in using it. I would consider using following options:
1)
Type variable;
if (condition1) variable = value1;
else if (condition2) variable = value2;
...
else throw new InvalidOperationException();
This has a disadvantage that in each line you have variable =
but anyway compiler will let you know if you haven't initialized variable
value before using it so it won't bother me much.
Also if your
value1
,value2
, etc cannot be null then I would consider using this:var variable = condition1 ? value1 : condition2 ? value2 : condition3 ? value3 : condition4 ? value4 : null; if (variable == null) throw new InvalidOperationException();
P.S.: Of course I cannot be sure without knowing the context but if you have to write such statements frequently then it is already seems to be wrong for me, maybe something may be changed on higher level?