IDE0180: Use Tuple to Swap Values

As I upgraded my Visual Studio 2022 Preview, I noticed a new code suggestion: IDE0180. It popped next to a reasonably standard variable swap:

Old swap code
var tmp = a;
a = b;
b = tmp;

The new syntax it recommended was much nicer, in my opinion:

New swap code
(a, b) = (b, a);

I love the new syntax!

Except it’s not new – it has been available since C# 7 (we’re at 10 now, just for reference). I just somehow missed it. The only reason I noticed it now was due to a suggestion. I guess better late than never. :)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.