结合了集合和字典语义的.Net数据结构

发布于 2024-10-07 19:50:41 字数 354 浏览 0 评论 0原文

.Net 是否有一个开箱即用的通用集合类型,它结合了 Set 和 Dictionary 语义,这样我只存储项目,但也可以通过从每个项目派生的某个键进行快速查找?像这样的东西:

var foos = new KeyedSet<string, Foo>(foo => foo.ID);

foos.Add(new Foo { ID = "X234", Descr = "One foo" });
foos.Add(new Foo { ID = "Q909", Descr = "Another foo" });

Console.WriteLine("Foo Q909 is {0}", foos["Q909"].Descr);

Is there an out-of-the-box generic collection type for .Net that combines Set and Dictionary semantics, such that I only store items, but can also do fast look-ups by some key that is derived from each item? Something like this:

var foos = new KeyedSet<string, Foo>(foo => foo.ID);

foos.Add(new Foo { ID = "X234", Descr = "One foo" });
foos.Add(new Foo { ID = "Q909", Descr = "Another foo" });

Console.WriteLine("Foo Q909 is {0}", foos["Q909"].Descr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文