How can a dictionary be output in the form ?:
key1 val1 val2 val3 key2 val1 val2 val3 Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
Dictionary itself means storing data in the form Key = Value, where one Key contains only one Value . But, the benefit of Value, we can contain almost any object, which means that we can easily add collections to it.
Here is an example of the simplest dictionary with an array:
Dictionary<string, string[]> dictionary = new Dictionary<string, string[]> { ["key1"] = new[] { "val1", "val2", "val3", }, ["key2"] = new[] { "val1", "val2", "val3", }, }; If you need to add something, then we can safely do the following:
dictionary.Add("key3", new []{"val1", "val2"}); Changing values, the simplest:
dictionary["key2"][0] = "val11"; Well, the conclusion of what you want:
foreach (var item in dictionary) { Console.WriteLine($"{item.Key}"); foreach (var val in item.Value) { Console.WriteLine(" " + val); } } Well, or we can pervert and make a conclusion through LINQ:
var result = dictionary.Aggregate(new StringBuilder(), (sb, kvp) => sb.AppendLine(kvp.Key).AppendLine(string.Join("\n", kvp.Value.Select(x => $" {x}"))), builder => builder.ToString()); Console.Write(result); Result:
key1 val1 val2 val3 key2 val1 val2 val3 var dictionary = new Dictionary<string, string[]>() { { "key1", new[] { "val1", "val2", "val3" } }, { "key2", new[] { "val1", "val2", "val3" } }, }; - tym32167List<Obj> , then you will get an error. It is better to group the original collection first, and only then translate it into a Dictionary, but this is another question ... - EvgeniyZSource: https://ru.stackoverflow.com/questions/874613/
All Articles
Dictionaryonly one value can be associated with a single key. Maybe you need aLookup, well, or use a list as a value in the dictionary. But most likely I did not understand the question. - Andrei NOP