I have a tuple array as shown below;
var games = new Tuple <string, Nullable<double>>[]
{ new Tuple<string, Nullable<double>>("Fallout 3: $", 13.95),
new Tuple<string, Nullable<double>>("GTA V: $", 45.95),
new Tuple<string, Nullable<double>>("Rocket League: $", 19.95) };
I am wondering if there is a function would let me add another item to this list.
Please help!
Thanks, Sean
Add/insert items to tuple If you want to add new items at the beginning or the end to tuple , you can concatenate it with the + operator as described above, but if you want to insert a new item at any position, you need to convert a tuple to a list. Convert tuple to list with list() . Insert an item with insert() .
Use the list() function(converts the sequence/iterable to a list) to convert both the input tuples into a list. Use the extend() function(adds all the elements of an iterable like list, tuple, string etc to the end of the list) to append or concatenate the above 2nd list to the first list.
Adding/Inserting items in a Tuple You can concatenate a tuple by adding new items to the beginning or end as previously mentioned; but, if you wish to insert a new item at any location, you must convert the tuple to a list.
A tuple is a data structure that has a specific number and sequence of values. The Tuple<T1,T2,T3> class represents a 3-tuple, or triple, which is a tuple that has three components. You can instantiate a Tuple<T1,T2,T3> object by calling either the Tuple<T1,T2,T3> constructor or the static Tuple.
use List
var games = new List<Tuple<string, Nullable<double>>>()
{
new Tuple<string, Nullable<double>>("Fallout 3: $", 13.95),
new Tuple<string, Nullable<double>>("GTA V: $", 45.95),
new Tuple<string, Nullable<double>>("Rocket League: $", 19.95)
};
games.Add(new Tuple<string, double?>( "Skyrim", 15.10 ));
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