26

In some languages (such as PHP, Haskell, or Scala), you can assign multiple variables from tuples in a way that resembles the following pseudocode:

list(string value1, string value2) = tupleWithTwoValues;

I can't find a way to do this in C#, however, without writing longer, uglier code:

string firstValue = tupleWithTwoValues.Item1;
string secondValue = tupleWithTwoValues.Item2;

This two-line solution is obviously not the end of the world, but I'm always looking for ways to write prettier code.

Does anyone know a better way to do this?

2

7 Answers 7

37

This is now available in C# 7:

public (string first, string last) FullName()
{
    return ("Rince", "Wind");
}

(var first, var last) = FullName();

You can even use a single var declaration:

var (first, last) = FullName();

More on destructuring tuples in the official documentation.

Sign up to request clarification or add additional context in comments.

Comments

7

Valid up to C# 6:

No, this is not possible. There's no such language feature in C#.

If you think the following code:

string firstValue = tupleWithTwoValues.Item1;
string secondValue = tupleWithTwoValues.Item2;

is ugly, then you should reconsider using tuples at the first place.


UPDATE: As of C# 7, tuple deconstruction is now possible. See the documentation for more information.

See Jared's answer as well.

5 Comments

I would add that "moving things around" is not something that you would do very frequently in C#. Unless you want to give the thing a shorter name, and use it many times in your code, you would probably access tupleWithTwoValues.Item1 directly instead of putting it in a variable.
True. Just seconds after posting that, it hit me that a richer solution (like a DTO) is often the way to go whenever I'm considering a tuple.
@Micah Yes, including semantics is always a plus. Tuples are usually a good thing in very specific scenarios only.
@Jared Thanks for pointing that out. Updated my answer, although back in 2014 that language feature hadn't existed.
3

You can technically do this with a single statement, rather than two statements, using the following syntax, although the character count is almost identical.

string firstValue = tupleWithTwoValues.Item1
    , secondValue = tupleWithTwoValues.Item2;

Comments

1

No this is not supported in C#, although others have suggested adding a feature like this (here and here).

It is supported by F#, however:

let (f, b) = ("foo", "bar")

1 Comment

1

Yes it is possible in C#. You'll need to install the package Value.Tuple in your project. You can do like this

List<Tuple<string,string>>() lstTuple = GetYourTupleValue();
foreach(var item in lstTuple)
{
  (string Value1, string Value2 ) = item;
}
Console.WriteLine(item.Value1);

Comments

0

when it comes to lists, you can do something like this:

var list = new List<string>{tuple.Item1, tuple.Item2};

(It's not that wordy) But for multiple variables, no. You can't do that.

Comments

0

This is what I do:

public static TResult Select<T1, T2, TResult>(this Tuple<T1, T2> source, Func<T1, T2, TResult> selector)
{
    return selector(source.Item1, source.Item2);
}
// this allows us ...
GetAssociationAndMember().Select((associationId,memberId) => {
    // do things with the aptly named variables
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.