简体   繁体   English

C#数据结构像字典但没有价值

[英]C# Data Structure Like Dictionary But Without A Value

Is there any data structure in C# that is like a dictionary but that only has a key and doesn't have a value. C#中是否有任何数据结构类似于字典但只有一个键而没有值。 I basically want a list of integers that I can quickly lookup and see if a certain value is in the list. 我基本上想要一个整数列表,我可以快速查找并查看列表中是否有某个值。 Granted, for my current use, a List would not cause any performance problem, but it just doesn't seem to fit well with the intent of what my code is doing. 当然,对于我目前的使用,List不会导致任何性能问题,但它似乎不符合我的代码所做的意图。

Yes, it's called a HashSet<T> , and available in version 3.5 of the .NET framework. 是的,它被称为HashSet<T> ,可在.NET框架的3.5版本中使用。 If you use .NET version 2.0, you can use a Dictionary and set values to null . 如果使用.NET 2.0版,则可以使用Dictionary并将值设置为null

If 3.5 is not an option you could do something like Dictionary < int, int > and simply ignore the value. 如果3.5不是一个选项,你可以做类似Dictionary <int,int>的操作,只需忽略该值。 i've done this in 2.0 and i tend to set the value to the same as the key. 我在2.0中完成了这个,我倾向于将值设置为与键相同。

如果您不是针对.NET 3.5,那么Power Collections (开源)也提供了Set实现。

或使用SortedList,其中值必须是唯一的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM