使用多个相同的键轻松查找值的最佳集合类型是什么?

Edw*_*uay 8 c# collections dictionary

我有像下面这样的文本文档,包含单个多个变量:

title:: Report #3
description:: This is the description.
note:: more information is available from marketing
note:: time limit for this project is 18 hours
todo:: expand the outline
todo:: work on the introduction
todo:: lookup footnotes
Run Code Online (Sandbox Code Playgroud)

我需要遍历这个文本文档的行并用这些变量填充一个集合,目前我正在使用一个Dictionary:

public Dictionary<string, string> VariableNamesAndValues { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是,这并不在工作,相同的键,如在上面的例子"音符"和"待办事项"由于键必须是唯一的解释.

什么是最好的集合,这样我不仅可以获得这样的单个值:

string variableValue = "";
if (VariableNamesAndValues.TryGetValue("title", out variableValue))
    return variableValue;
else
    return "";
Run Code Online (Sandbox Code Playgroud)

但是我也可以像这样得到多个值:

//PSEUDO-CODE:
List<string> variableValues = new List<string>();
if (VariableNamesAndValues.TryGetValues("note", out variableValues))
    return variableValues;
else
    return null;
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 7

如果您的键和值是字符串,那么使用NameValueCollection.它支持给定键的多个值.

它不是世界上最有效的收藏品.特别是因为它是一个非泛型类,使用了大量的虚方法调用,而GetValues方法将为其返回值分配数组.但是,除非你需要最好的表演集合,这当然是最方便的集合,做你的要求.