Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# multi assignment

int a, b, n;
...
(a, b) = (2, 3);
// 'a' is now 2 and 'b' is now 3

This sort of thing would be really helpfull in C#. In this example 'a' and 'b' arn't encapsulated together such as the X and Y of a position might be. Does this exist in some form?

Below is a less trivial example.

(a, b) = n == 4 ? (2, 3) : (3, n % 2 == 0 ? 1 : 2);

Adam Maras shows in the comments that:

var result = n == 4 ? Tuple.Create(2, 3) : Tuple.Create(3, n % 2 == 0 ? 1 : 2);

Sort of works for the example above however as he then points out it creates a new truple instead of changing the specified values.

Eric Lippert asks for use cases, therefore perhaps:

(a, b, c) = (c, a, b); // swap or reorder on one line
(x, y) = move((x, y), dist, heading);
byte (a, b, c, d, e) = (5, 4, 1, 3, 2);
graphics.(PreferredBackBufferWidth, PreferredBackBufferHeight) = 400;

notallama also has use cases, they are in his answer below.

like image 261
alan2here Avatar asked Nov 28 '11 17:11

alan2here


2 Answers

We have considered supporting a syntactic sugar for tuples but it did not make the bar for C# 4.0. It is unlikely to make the bar for C# 5.0; the C# 5.0 team is pretty busy with getting async/await working right. We will consider it for hypothetical future versions of the language.

If you have a really solid usage case that is compelling, that would help us prioritize the feature.

like image 137
Eric Lippert Avatar answered Oct 22 '22 03:10

Eric Lippert


use case:

it'd be really nice for working with IObservables, since those have only one type parameter. you basically want to subscribe with arbitrary delegates, but you're forced to use Action, so that means if you want multiple parameters, you have to either use tuples, or create custom classes for packing and unpacking parameters.

example from a game:

public IObservable<Tuple<GameObject, DamageInfo>> Damaged ...

void RegisterHitEffects() {
    (from damaged in Damaged
     where damaged.Item2.amount > threshold
     select damaged.Item1)
    .Subscribe(DoParticleEffect)
    .AddToDisposables();
}

becomes:

void RegisterHitEffects() {
    (from (gameObject, damage) in Damaged
     where damage.amount > threshold
     select gameObject)
    .Subscribe(DoParticleEffect)
    .AddToDisposables();
}

which i think is cleaner.

also, presumably IAsyncResult will have similar issues when you want to pass several values. sometimes it's cumbersome to create classes just to shuffle a bit of data around, but using tuples as they are now reduces code clarity. if they're used in the same function, anonymous types fit the bill nicely, but they don't work if you need to pass data between functions.

also, it'd be nice if the sugar worked for generic parameters, too. so:

IEnumerator<(int, int)>

would desugar to

IEnumerator<Tuple<int,int>>
like image 24
notallama Avatar answered Oct 22 '22 03:10

notallama