Conditional Coalescing and Assignment Operators #7897
-
Summary:I would like to propose the addition of conditional assignment operators >?= and <?= to improve the expressiveness and conciseness of conditional assignments in C#. Motivation:Currently, conditional assignments often involve an extra line of code or ternary operators, making the code less readable and more verbose. The proposed operators aim to enhance code readability and reduce verbosity in certain scenarios. Syntax Proposal int x = 10;
int y = 5;
x >?= y; // Expands to x = x > y ? x : y
x <?= y; // Expands to x = x > y ? x : y
Console.WriteLine(x >? y); // Expands to Console.WriteLine(x > y ? x : y);
Console.WriteLine(x <? y); // Expands to Console.WriteLine(x < y ? x : y); Additional InformationImpact on Existing Code: Alternatives Considered: Conclusion Edits: Fixed formatting mistakes |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
using static System.Math;
x = Max(x, y);
x = Min(x, y); |
Beta Was this translation helpful? Give feedback.
-
Making C# cryptic has never been a goal of the LDM folks. Including a Reducing character count isn't of itself a sufficient goal either. Check out -100 points. |
Beta Was this translation helpful? Give feedback.
Making C# cryptic has never been a goal of the LDM folks. Including a
?
might be necessary to avoid collision with other operators, but there's nothing here about null tolerance, which is what?
is often used for - so these would be very confusing symbols.Reducing character count isn't of itself a sufficient goal either.
Check out -100 points.