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?
Don't forget the ()
when you cast:
Tuple<string, string> selectedTuple =
(Tuple<string, string>)comboBox1.SelectedItem;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With