How do you cast an object to a Tuple?

21,939

Solution 1

Don't forget the () when you cast:

Tuple<string, string> selectedTuple = 
                  (Tuple<string, string>)comboBox1.SelectedItem;

Solution 2

As of C# 7 you can cast very simply:

var persons = new List<object>{ ("FirstName", "LastName") };
var person = ((string firstName, string lastName)) persons[0];

// The variable person is of tuple type (string, string)

Note that both parenthesis are necessary. The first (from inside out) are there because of the tuple type and the second because of an explicit conversion.

Solution 3

Your syntax is wrong. It should be:

Tuple<string, string> selectedTuple = (Tuple<string, string>)comboBox1.SelectedItem;

Alternatively:

var selectedTuple = (Tuple<string, string>)comboBox1.SelectedItem;
Share:
21,939

Related videos on Youtube

Alexandru
Author by

Alexandru

"To avoid criticism, say nothing, do nothing, be nothing." - Aristotle "It is wise to direct your anger towards problems - not people; to focus your energies on answers - not excuses." - William Arthur Ward "Science does not know its debt to imagination." - Ralph Waldo Emerson "Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game." - Donald Trump "All our dreams can come true, if we have the courage to pursue them." - Walt Disney "Mitch flashes back to a basketball game held in the Brandeis University gymnasium in 1979. The team is doing well and chants, 'We're number one!' Morrie stands and shouts, 'What's wrong with being number two?' The students fall silent." - Tuesdays with Morrie

Updated on October 27, 2020

Comments

  • Alexandru
    Alexandru over 3 years

    I create my Tuple and add it to a combo box:

    comboBox1.Items.Add(new Tuple<string, string>(service, method));
    

    Now I wish to cast the item as a Tuple, but this does not work:

    Tuple<string, string> selectedTuple = 
                       Tuple<string, string>(comboBox1.SelectedItem);
    

    How can I accomplish this?

  • Alexandru
    Alexandru over 11 years
    Thanks! I got lost in all the brackets. I'll mark your post as the best answer once the timer expires!
  • TYY
    TYY over 11 years
    Or Tuple<string, string> selectedTuple = (comboBox1.SelectedItem as Tuple<string, string>);
  • Rune FS
    Rune FS over 11 years
    @TYY which will hide an error if for some unexpected reason it's not a tuple. If you know something is of type T then use a cast, if you know that something might be of type T but it's valid bahavior even if it's not use as and a null test
  • TYY
    TYY over 11 years
    @Rune FS Well depending on how he is using the tuple object, a check on null could make sense vs throwing an exception. Just giving him another option even though I know Cedric's answer is absolutely correct.
  • Rune FS
    Rune FS over 11 years
    @TYY yes that's exactly what I state that in the case he knows it should always be the specified tuple type cast is the appropriate option (because if the cast fails it is then by definition an exceptional situation) if the object could legally be something else asis the appropriate option
  • Jess Rod
    Jess Rod over 2 years
    I'd rather use: var person = ((string, string)) persons[0];
  • DomenPigeon
    DomenPigeon over 2 years
    Yes that's also possible but in this case you you would need to use person.Item1 and person.Item2 instead of person.firstName and person.lastName