如何在字典中找到最小键

Por*_*sin 8 c# dictionary

我声明字典如下:

private Dictionary<int, touchInformation> touchDictionary = new Dictionary<int, touchInformation>();
Run Code Online (Sandbox Code Playgroud)

我使用如下:

touchDictionary[touchID] = touchObject;

因此,touchDictionary将保留来自touchID的密钥.现在,我尝试使用字典找到最小密钥,但我不知道该怎么做.有什么建议吗?

请注意,C.Porawat

Col*_*inE 20

Dictionary具有Keys属性,允许您枚举字典中的键.您可以使用Min Linq扩展方法获取最小密钥,如下所示:

int minimumKey = touchDictionary.Keys.Min();
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这是一个O(n)操作.你没有在这里获得字典的好处. (4认同)
  • 原始问题中没有任何内容表明这是性能敏感的操作! (3认同)
  • 也许将您的字典包装在一个类中,该类可以记录用于创建条目的最小密钥? (2认同)