列表<>作为TValue的字典

Kri*_*s-I 1 .net c#

我想创建一个Dictionary,TKey是一个字符串,TValue是一个 List<DateTime>

我怎样才能做到这一点 ?

谢谢,

Luc*_*ero 10

Dictionary<string, List<DateTime>> dict = new Dictionary<string, List<DateTime>>();
Run Code Online (Sandbox Code Playgroud)

请注意,添加新项目时,需要为该值分配新列表:

string key; // assuming that's your key
List<DateTime> value;
if (!dict.TryGetValue(key, out value)) {
    value = new List<DateTime>();
    dict.Add(key, value);
}
// value is now always a valid instance
Run Code Online (Sandbox Code Playgroud)