c# - 通过部分键c#字典获取值

标签 c# dictionary

我有这本字典。

private Dictionary<string[], ICommand> commandsWithAttributes = new Dictionary<string[], ICommand>();

我需要通过部分键在 commandsWithAttributes 中找到元素。我的意思是:

“-?” - 是我用来查找项目的键。

({"-t","--thread"},ICommand)

({"-?","--help"},ICommand) <- 这就是我需要找到的。

最佳答案

请不要这样做。字典针对一键到一值搜索进行了优化。

我建议对单个值使用多个键如下:

private Dictionary<string, ICommand> commandsWithAttributes = new Dictionary<string, ICommand>();

var command1 = new Command(); //Whatever

commandsWithAttributes.Add("-t", command1);
commandsWithAttributes.Add("--thread", command1);

var command2 = new Command(); //Whatever

commandsWithAttributes.Add("-?", command2);
commandsWithAttributes.Add("--help", command2);

关于c# - 通过部分键c#字典获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369237/

相关文章:

c# - VSCode C# "go to definition"(F12) 不起作用

c++ - 如何将 char* 插入到 map<string,string> 中?

c# - 为什么 ExpandoObject 比 Dictionary 慢很多?

hadoop - Hadoop MapReduce WordCount 如何将输入作为 <key, value> 对?

python - 通过关联值的函数过滤字典键

python - 在 Python 中检查 CSV 的第一个单元格是否为空

c# - 单击生成的 html 组件调用方法

c# - 使用 IClientMessageInspector 从 WCF soap 请求中删除操作节点 mustUnderstand

c# - 使用 Flurl 向 StackExchange API 发出 GET 请求时出现 "Unexpected character exception"

c# - 无法识别方法 'System.String ToString()' 方法,并且无法将此方法翻译成存储表达式