5

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

Tech Dynasty
  • 163
  • 1
  • 4
  • 11

4 Answers4

26

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 ));
Nino
  • 6,931
  • 2
  • 27
  • 42
  • Awesome, worked! will mark as answer when it lets me (: – Tech Dynasty Mar 14 '17 at 12:12
  • 1
    @TechDynasty On a couple of sidenotes the string in the tuple should just be the game name, you can format the spaces and `$` in later; so far there doesn't seem to be much use to be using a nullable double; the amount should really be a decimal not a double and it would probably be best to use a custom class for this i.e. `Game` – TheLethalCoder Mar 14 '17 at 12:14
  • 1
    glad i could help. btw, if your project gets complicated (and it surely will) think of using classes for data instead of Tuples. You'll have cleaner code easier to maintain. – Nino Mar 14 '17 at 12:15
2

Although the other answers already cover what you should be doing i.e. using a List (code taken from this answer):

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)
};

And then you can call the Add method:

games.Add(new Tuple<string, double?>("Skyrim:    $", 15.10));

I'd like to point out a few things you can do to improve your code.

  1. The string in the Tuple should just be the game name, you can always format it later:

    string formattedGame = $"{game.Item1}:    ${game.Item2}";
    
  2. There doesn't seem to be much need for using Nullable<double> (can also be written as double? BTW), consider just using a double.

  3. When dealing with monetary values it is advisable to use decimal, so consider switching to that.
  4. Consider using a custom class i.e. Game. This will simplify the code and help later on when you want to add more details i.e. Description, Genre, AgeRating etc.

For more detail on when to use an array or a list see this question, however, the short version is you should probably be using a list.

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
1

Use Resize method:

Array.Resize(ref games, games.Length + 1);
games[games.Length - 1] =  new Tuple<string, Nullable<double>>("Star Flare: $", 5.00),;
AgentFire
  • 8,944
  • 8
  • 43
  • 90
0
class Program
{
    static void Main(string[] args)
    {
        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) };

        Array.Resize(ref games, 4);
        games[3] = new Tuple<string,double?>("Test", 19.95);
    }
}