Join two different string arrays in C#

Playstation Gamer 41 Reputation points
2022-05-08T10:39:33.557+00:00

Hi. I'd like to see if I can join two different string arrays together.

I have these two strings:
string[] AdditionalExtras = new[] { "1 - 16” Alloys", "2 - Privacy Glass", "3 - Spare Wheel", "4 - Parking Pack", "5 - SYNC 3 with Navigation", "6 - Winter Pack", "7 - Floor mats", "8 - Boot liner", "9 - Dashboard camera", "10 - Pet travel mat" }; // Any additional extras will be displayed in the array
string[] ExtrasCost = new[] { "£300", "£250", "£150", "£600", "£600", "£350", "£70", "£61.69", "£284.36", "£264.50" }; // Cost of each extra item

And, I would like them to be displayed like this (if possible):
16” Alloys - £300
Privacy Glass - £250

The prices are already laid out to correspond with each number within the 'AdditionalExtras' array, if that makes sense. So, item 1 will cost £300, item 2 will cost £250 and so on...

To clarify: If the user enters 1 and 2, I would only like those items to be displayed, not the whole list!

I did try to work out a solution on my own, however, nothing I could come with seems to work (my screenshots below should show this). I also looked online for some solutions, but didn't find anything that would've helped me.

Note: I have asked a previous question based on the first array, which can be found here, so I have got the first array working and display how I want to (my code is pretty similar to the answer that was provided).

199967-screenshot-2022-05-08-113410.png

199983-screenshot-2022-05-08-113633.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-05-09T03:09:55.197+00:00

    @Playstation Gamer , Welcome to Microsoft Q&A, based on my test, only a few changes will get what you wanted.

    Please change the following code:

    while (true)  
                {  
                    Console.WriteLine("Please select any of the extras that you would like to purchase!");  
      
                    int ExtraSelection = Convert.ToInt32(Console.ReadLine());  
      
                    if (dic.ContainsKey(ExtraSelection))  
                    {  
                        list.Add(dic[ExtraSelection]+" ---- "+ ExtrasCost[ExtraSelection-1]);// here you need to add  
                    }  
                    else  
                    {  
                        Console.WriteLine("Please input the correct number");  
                    }  
                    ConsoleKeyInfo c = Console.ReadKey();  
                    if (c.Key == ConsoleKey.Spacebar)  
                    {  
                        break;  
                    }  
      
                }  
    

    Tested result:
    200234-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-05-08T10:51:02.587+00:00

    Try this code:

    var result = AdditionalExtras.Zip( ExtrasCost, ( e, c ) => $"{e} - {c}" );
    
    foreach( var r in result )
    {
        Console.WriteLine( r );
    }
    

  2. Karen Payne MVP 35,116 Reputation points
    2022-05-08T15:15:24.487+00:00

    Using arrays is really not the path to take, instead use classes/models e.g. the following is a conceptual starter to expand on as needed. Data should not be stored in code but instead either comma-delimited text file, json file or database.

    public class Operations
    {
        public static List<Extra> AdditionalItems = new();
        public static string FileName => "TODO";
        public static void Add(Extra sender)
        {
            AdditionalItems.Add(sender);
        }
        public static void Remove(Extra sender)
        {
            AdditionalItems.Remove(sender);
        }
    
        public static void Clear()
        {
            AdditionalItems.Clear();
        }
    
        public static void Save()
        {
            // save AdditionalItems if needed using FileName
        }
    
        public static void ReadFromFile()
        {
            // read from FileName into AdditionalItems
        }
    }
    
    public class Extra
    {
        public int Id { get; set; }
        public string ItemName { get; set; }
        public decimal Cost { get; set; }
        public int Quantity { get; set; }
        public override string ToString() => ItemName;
    
    }
    

    In regards to the menu check out Spectre.Console which provides a wealth of methods to work with console projects.