Subhadeep
Subhadeep

Reputation: 30

Adding Data in List inside a Dictionary in C#

Is there any process or shortcut to add new values to this pre-existing list directly inside dictionary without updating it?

Only have to write the code inside Main. Rest was hardcoded in the compiler & can't be changed. Your Help will be appreciated. Suggestions are welcome :)

using System;
using System.Collections.Generic;

namespace AddNewMember             
{
    public class Club         
    {

        static Dictionary<int, string> groupInfo = new Dictionary<int, string>() { { 1, "Gold" }, { 2, "Silver" }, { 3, "Platinum" } };
        static Dictionary<int, List<String>> memberInfo = new Dictionary<int, List<String>>() {
                                    { 1, new List<string>(){ "Tom","Harry"} },
                                    { 2,new List<string>(){ "Sam","Peter"} },
                                    { 3,new List<string>(){ "Kim","Robert"} } };

        public static void Main(string[] args)        
        {
        //Write your code here. Above part is hardcoded can't be changed
            Console.WriteLine("Group Name :");
            string gName = Console.ReadLine();
            int num = 0;

            foreach (KeyValuePair<int, string> VARIABLE in groupInfo)
            {
                if (VARIABLE.Value == gName)
                {
                    num = VARIABLE.Key;
                }
            }

            Console.WriteLine("Member Name:");
            string name = Console.ReadLine();


        //Step 1
            List<string> l = memberInfo[num];
            l.Add(name);

        //Step 2
            memberInfo[num] = l;

       //Step 3
            List<string> r = memberInfo[num];
            foreach (var VARIABLE in r)
            {
                Console.WriteLine(VARIABLE);
            }

        }
    }
}

Upvotes: 1

Views: 2492

Answers (2)

Caius Jard
Caius Jard

Reputation: 74605

Seems to me like your understanding of dictionaries is upside down. You use the key to retrieve the value, not the other way round. If you're looking to have the user enter a group name (gold, silver, bronze) and then the name of the person to add to that group, you should make the dictionary map a string (group name) to a list of members

static Dictionary<string, List<String>> groupInfo = new() { 
  { "Gold", new(){ "Tom","Harry" } },
  { "Silver", new(){ "Sam","Peter"} },
  { "Platinum", new(){ "Kim","Robert"} }
};

public static void Main(string[] args)        
{
    Console.WriteLine("Group Name :");
    string gName = Console.ReadLine();
        
    Console.WriteLine("Member Name :");
    string mName = Console.ReadLine();
        
    groupInfo[gName].Add(mName);

}

Yep, that's it. GroupInfo maps string group names to list of string member names, calling groupInfo[gName] resolves to a List of string, so the Add method call there is being performed on the list and the given member name is added

Side note, I'm leveraging a facility of recent c# where you don't have to repeat the type name on both sides of the =. The compiler will know that groupInfo is a Dictionary<string, List<string>> and when it seems new() in the initializer it knows I mean new List<string> which can really help tidy things up. The parentheses are necessary otherwise it would think I was trying to make an anonymous type, which is a different thing. If you get compiler errors you might have to restore the type names if your c# is older

Upvotes: 2

Le Vu
Le Vu

Reputation: 437

We don't need to re-assign modified list into dictionary value. Step #2 is redundant. When you retrieve the list from step #1. It return a pointer (reference) to the list in the dictionary. It means, when you perform insert an item into the list variable, the list in dictionary is updated (new item added).

Also, In step #3, you get the r but not used.

Upvotes: 0

Related Questions