Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an item to a Tuple in C# [closed]

Tags:

c#

tuples

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

like image 345
Tech Dynasty Avatar asked Mar 14 '17 12:03

Tech Dynasty


People also ask

How do I add an item to a tuple?

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() .

How do I add a tuple to a tuple?

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.

Can we insert in tuple?

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.

Can a tuple have 3 items?

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.


1 Answers

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 ));
like image 75
Nino Avatar answered Nov 15 '22 07:11

Nino