Right now I can do this:
public static void Swapper (ref int a, ref int b) {
b = (a = a + b) - b;
a = a - b;
}
But I want to be able to do this:
public static void Swapper (ref int a, ref int b) {
a = (b = a);
}
So, I want the assignment expression to return the original value of the left operand before it gets changed. This is a syntax-related question, and I'm looking for a general solution that works for all cases, not just the specific example provided. Is this possible?
Edit
I'm aware of:
(a, b) = (b, a);
This specific case is just an example used to illustrate what I want syntax-wise. Is just a dummy method. I don't have a need to swap two variables.
Basically I have ++i
and I want i++
.
Edit 2
In Python the functionality for returning the value in an assignment expression is achieved by the walrus operator :=
. Let's assume this new operator that returns the left-hand side first is expressed like =:
, a reverse walrus operator.
Then when traversing some kind of linked list, for example, we could do (in C#):
while (x != null && y != null) {
node = (node.next = (x.value > y.value) ? new Node(x =: x.next) : new Node(y =: y.next));
}
vs
while (x != null && y != null) {
if (x.value > y.value) {
node = (node.next = new Node(x));
x = x.next;
} else {
node = (node.next = new Node(y));
y = y.next;
}
}
Anyway, this is probably convoluted and rarely useful, but seems a bit more elegant to me.