-1

In my recent project im trying to make a dictionary with key as a string and value as List of string(List) and adding value in dictionary using for loop , but the problem is that after first iteration when I Update the List for second iteration it is automatically changing in the first key value pair. for example in first iteration it is saving key as apple and value as list {cucumber,chilli,tomato,apple} its fine but after first iteration when i update list to {cucumber,chilli,tomato,apple,mango} and saving it to second key mango it is also updating the first value to {cucumber,chilli,tomato,apple,mango}.

var mylist = new List<string>()
            {
                "cucumber",
                "chilli",
                "tomato"
            };
            var yourlist = new List<string>()
            {
                "apple",
                "mango",
                "banana"
            };
            var dict = new Dictionary<string, List<string>>();
            foreach (var i in yourlist)
            {
                mylist.Add(i);
                dict.Add(i,mylist);
            }
            foreach(var d in dict.Keys)
            {
                foreach(var l in dict[d])
                {
                    Console.WriteLine(l);
                }
            }
  • Issue is mentioned by Caius in his answer. But it’s not only about solving that issue. But it looks very weird by looking at code and I didn’t get that what you are trying to achieve here by this code? Could you please explain – Vivek Nuna Nov 20 '21 at 20:22
  • You need to learn [the difference between a reference type and value type?](https://stackoverflow.com/questions/5057267/) – Dour High Arch Nov 20 '21 at 21:27
  • actually I want to add keys which is username and value from another table which is list of access so for every iteration Im changing the value which is list of string(access) but it is giving last updated list of access to all values associated to keys – Nomaan Pansare Nov 21 '21 at 06:35
  • this is my acual code – Nomaan Pansare Nov 21 '21 at 06:36
  • for(int i = 0; i < user.Count(); i++) { var ll = accessRightForum.Where(x => x.UserRowId == user[i].RowID).ToList(); foreach (var j in ll) { var forumName = forum.Where(x => x.RowId == j.ForumRowId).FirstOrDefault().ForumName; forumList.Add(forumName); } dict[user.ElementAt(i).FirstName] = forumList; forumList.Clear(); } – Nomaan Pansare Nov 21 '21 at 06:50
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 21 '21 at 08:45

1 Answers1

1

The dictionary entries' Value properties are always the same list, so anything you do to one, ends up showing in all of them (because there is only one)

Take a look at the code below; if you understand why a and b here both show the same change, then you should understand that your dictionary scenario is essentially the same

var list = new List<string>(){ "hello", "world" };
var a = list;
var b = list;

a.Add("there");

Console.Write(b.Count); //shows 3

If you don't understand why a, b and list above all refer to the same list, then drop a comment and I'll add some more explanation


As to what you should do about your "issue", it's not entirely clear to me what you're hoping to do but if you made sure that each key in the dictionary associated with a new list rather than the same one, then changes to one key's list would not show in other keys' lists:

dict.Add(i, new List<string>(mylist));

This makes a new list per key, and initializes the new list with the items present in mylist at the time (my list grows each pass of the loop)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80