How can I print the content of my dictionary and check when IDs are same. Like for example I get an Id like B83773, if it's appear in the dictionary I would print it on console.
Any kind of help will be super appreciated, I do not really understand the concept.
static void Main(string[] args) { string ExtractIDFromFileName(string filename) { return filename.Split('_').Last(); } Dictionary<string, int> GetDictOfIDCounts() { List<string> allfiles = Directory.GetFiles("C:/filet/", "*.wish").Select(Path.GetFileNameWithoutExtension).ToList(); allfiles.AddRange(Directory.GetFiles("C:/filet/", "*.exe").Select(Path.GetFileNameWithoutExtension).ToList()); Dictionary<string, int> dict = new Dictionary<string, int>(); foreach (var x in allfiles) { string fileID = ExtractIDFromFileName(x); if (dict.ContainsKey(fileID)) { dict[fileID]++; } else { dict.Add(fileID, 1); } } return dict; } Console.WriteLine("" ); Console.ReadLine(); }
if (dict.ContainsKey(fileID)), what other than this is it that you require? Are you just missing aConsole.WriteLinestatement inside the{ ... }for that if-statement, is that it?dict.ContainsKey(fileID)checks if the key already exists in the dictionary, and returnstrueif it does,falseif it doesn't.Console.WriteLine("{0}:{1}", fileID,dict[fileID] );But I don't get why these up-votes for an unclear question