So I got C# foreach loop which goes like this
foreach(Type s in source)
{
dic1.Clear();
dic1.Add(Key1, s.Attr1);
dic1.Add(Key2, s.Attr2);
...
dic1.Add(KeyN, s.AttrN);
dic2.Add(ind, dic1);
ind++;
}
It gets the values and sets them correctly, no problem in that.
But when the dic1.Clear() is called it clears the dic1 which was added in the dic2
and rewrites the contents of it with the new data being handled resulting N amount of dictionaries with the same data from the last handled data/object. Does the dic1.Clear() really affect the one which was added into the dic2? If it does should I just copy dic1 into dicTemp and add that one into dic2?
Thank you for advance.
I think I get your question now.
When you add a reference object to a collection you are storing a reference to the object, not the value.
What you should do is make
dic1local to the loop body.