I can't add elements correctly to a Dictionary<string, List<string>>
with a List<string>
like value. Every time I enter a new key with a List as value, the values of the previous keys are overwritten with the last List entered.
Dictionary<string, List<string>> videosBloqueDict = new Dictionary<string, List<string>>();
List<string> videosResult = new List<string>();
string[] bloques = Directory.GetDirectories(path).Select(Path.GetFileName).ToArray();
foreach(var bloque in bloques)
{
string[] videos = Directory.GetFiles(path "\\" bloque).Select(Path.GetFileName).ToArray();
videosResult.Clear();
foreach (var video in videos)
{
string[] val = video.Split(' ').Skip(1).ToArray();
string result = string.Join(" ", val);
videosResult.Add(result); **//list with the new values to enter the dictionary**
}
videosBloqueDict.Add(bloque, videosResult);
}
For example:
string[] bloques = {01:00, 02:00, 03:00} (Keys I will add)
Actual Dictionary:
- Key = 01:00, Value = {yx, yy, xxx}
- Key = 02:00, Value = {yx, yy, xxx}
- Key = 03:00, Value = {yx, yy, xxx}
Expected Dictionary:
- Key = 01:00, Value = {xxx, yxy}
- Key = 02:00, Value = {xyy, yyx, yyy, yyx, yy}
- Key = 03:00, Value = {yx, yy, xxx}
CodePudding user response:
Lists are reference types, so videosResult
is a reference to a single list. Every turn of the loop, you clear the same list, and then add another reference to that same list to the dictionary. That's why you end up with identical results for each entry in the dictionary: it's the same list repeated multiple times.
You need to create a new list on each loop, and while you're at it, since you don't actually need the list itself outside of the loop, you should declare it in there directly:
// don't declare videosResult here
foreach(var bloque in bloques)
{
string[] videos = ...;
var videosResult = new List<string>(); // create a new list instead of clearing the original one
// rest of the method
}
By the way, you don't need so many calls to ToArray()
: if you're going to immediately start enumerating those values, just leave them as IEnumerable<T>
and, well, enumerate them. ToArray
creates unnecessary copies here, since it should only be used when you really need an array.
CodePudding user response:
Dictionary<string, List<string>> videosBloqueDict = new Dictionary<string, List<string>>();
string[] bloques = Directory.GetDirectories(path).Select(Path.GetFileName).ToArray();
foreach(var bloque in bloques)
{
List<string> videosResult = new List<string>();
string[] videos = Directory.GetFiles(path "\\" bloque).Select(Path.GetFileName).ToArray();
foreach (var video in videos)
{
string[] val = video.Split(' ').Skip(1).ToArray();
string result = string.Join(" ", val);
videosResult.Add(result); **//list with the new values to enter the dictionary**
}
videosBloqueDict.Add(bloque, videosResult);
}