Proposal: Conditional operator inside condition #2697
-
Hi, I've encountered situation where this might be lovely syntax sugar if possible. For example I have condition statement where I would compare two equal items either with <= or < operators depending on some other condition. Current solution is:
Proposed solution
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
if (itemA < itemB || (!someCondition && itemA == itemB))
{
} That's in my eyes more readable: "Go on, if itemA is smaller than itemB. If some condition is not fulfilled, we also accept equality of itemA and itemB." |
Beta Was this translation helpful? Give feedback.
-
Sounds like a very niche use case. I suggest just making a method or a custom |
Beta Was this translation helpful? Give feedback.
-
I agree it might be something we are mostly used to, but I had to think for a second about your solution to verify its corectness instead having instant readability which ternary operators might provide. .. Im fan of ternary, maybe thats why :) and we are excluding double itemA and itemB specification. |
Beta Was this translation helpful? Give feedback.
Sounds like a very niche use case. I suggest just making a method or a custom
IEqualityComparer
if you're doing this frequently.