-4

I was going through few examples and came across below code:

(a, b) = (b, a);

It was mentioned that this can be used to swap the values of the two variables.

I tried it out as below:

int a = 5, b = 10;
Console.WriteLine(a + " " + b); // Prints --> 5 10
  
(b, a) = (a, b);
Console.WriteLine(a + " " + b); // Prints --> 10 5

What is this syntax? Is it something new or is this some weird trick to get the swapping result. Can it also be used with any number of variables.

Like:

(a, b, c) = (c, a, b); // a=c; b=a; c=b; or even more variables
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jasmeet
  • 1,315
  • 11
  • 23
  • Not an answer to your question but: it's a form of syntactic sugar. This probably isn't a solution for the "swap two integers without a temp variable" problem because of this. See [SharpLab](https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UkALIgLIAUAlJtrjQJYB2ALogIaIC8iArADSIBGHRBAAMAbmo0cICAE5SbANSIARKsTL+5MYgD0uxAAUATg0YBnRAForAPm7CRsSVNL8+LSpwV8tEmFK4MvJK6mqa2noGJmaWNvai3C4AvrDJQA===). – ProgrammingLlama Jul 22 '20 at 05:37
  • swapping without temp variable is **impossible**. all you can do is let the _compiler_ worry about creating the temp variable - but it's still there. – Franz Gleichmann Jul 22 '20 at 05:41
  • The syntax is simply constructing a tuple with the original values, then deconstructing the tuple into the two target variables. See [this answer](https://stackoverflow.com/a/39190792) or [this answer](https://stackoverflow.com/a/45249664) in duplicate. – Peter Duniho Jul 22 '20 at 05:43
  • 1
    @Franz: _"swapping without temp variable is impossible"_ -- not strictly true. With a fixed constant and math, it is possible to swap values in two variables. One might consider the fixed constant a "variable" itself, but however you look at it, it's not a _temp_ variable (i.e. temporary storage for one of the values). – Peter Duniho Jul 22 '20 at 05:45
  • 1
    @FranzGleichmann `int a = 5; int b = 3; a = a ^ b; b = b ^ a; a = a ^ b;` - Swaps `a` and `b` without an additional variable. – ProgrammingLlama Jul 22 '20 at 05:50
  • @John now try that with objects ;) – Franz Gleichmann Jul 22 '20 at 06:11
  • @Franz Fortunately the topic isn't objects. This is sometimes an interview question, by the way. – ProgrammingLlama Jul 22 '20 at 06:23

1 Answers1

1

It creates a tuple on the right hand side of the = then deconstructs it (into different variables) on the left hand side. See this page of the fine manual

Effectively the compiler is writing something like this for you, if you're familiar with the Tuple<T1,T2> introduced years ago:

var t = new Tuple<int,int>(a, b); //create with Item1 = a and Item2 = b
b = t.Item1;
a = t.Item2;

Exactly what code it will be writing under the hood, I don't know, but that will be the spirit of it; to create a temporary (probably Value)Tuple, assign a and b to it then get them out again in the opposite order

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 1
    Two points: 1) I don't see the point in showing the syntax _as if_ the `Tuple` class was used, given that one could just as easily _show the real syntax_, using the actual `ValueTuple` type that's being used, and 2) the code you posted is illegal and would not compile. – Peter Duniho Jul 22 '20 at 06:11
  • @PeterDuniho the aim in using the older Tuple is that, having been around longer people are more likely to be familiar with it. I don't personally see the point in trying to explain a unfamiliar concept of what something is doing using another thing with which the user is unfamiliar. Tuple and ValueTuple are similar enough that one could read the code for one and understand it as the other, but I'm trying to avoid the "hangon, I know what a Tuple is, but what's a ValueTuple? How does it differ? (Googles) Oh.. it kinda doesn't in this case" in the OP's mind – Caius Jard Jul 22 '20 at 06:32