【发布时间】:2025-12-02 17:30:01
【问题描述】:
我有 text 文档,如下所示,其中包含 single 和 multiple 变量:
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
我需要遍历这个文本文档的行并用这些变量填充一个集合,目前我正在使用一个字典:
public Dictionary<string, string> VariableNamesAndValues { get; set; }
但这不适用于多个、相同键,例如上面示例中的“note”和“todo”,因为键必须是唯一的 在字典中。
什么是最好的集合,这样我不仅可以得到这样的单一值:
string variableValue = "";
if (VariableNamesAndValues.TryGetValue("title", out variableValue))
return variableValue;
else
return "";
但我也可以像这样得到多个值:
//PSEUDO-CODE:
List<string> variableValues = new List<string>();
if (VariableNamesAndValues.TryGetValues("note", out variableValues))
return variableValues;
else
return null;
【问题讨论】:
标签: c# collections dictionary