``new dictionary< string,obj>(stringcomparer.currentcurtultureignorecase)`/`新的hashset&lt string>(stringcomparer.currentCurrentCurtultureignorignOgnoregecase)
我正在寻找一种方法来让字典键/哈希集字符串
不敏感。我在So甚至MS网站上发现了许多答案。
据我所知,似乎答案是在说iequalityComparer< tkey>
string> stringcomparer.currentcurentculturendureignorecase
(或类似的内容) 和hashset
,这将使它仿佛将密钥集不敏感。但是不,他们说的是设定平等运算符,这是不同的。
我熟悉将密钥对象转换为哈希数字值的哈希集,然后将其用于访问 May 包含您要寻找的密钥的存储桶。然后,将平等运算符用于检查该存储桶实际上是否包含您要寻找的值。
如果是这种情况,则可以想象a
和a
可以在不同的存储桶中,因此可以添加这两个项目而不会发生冲突,或者检查和检查找不到另一个。我必须假设事实并非如此,因为这样可以打破很多代码,那么这里到底发生了什么?幕后发生了什么?
I was looking around for a way to have a dictionary key/hash set string
be case insensitive. I found many answers around on SO and even on the MS website.
From what I've seen, it seems like the answers are saying that by setting the IEqualityComparer<TKey>
to StringComparer.CurrentCultureIgnoreCase
(or something similar) for Dictionary
and HashSet
, this will make it as if the key set will be case insensitive. But no, they are saying that it sets the equality operator, which is different.
Hash sets that I'm familiar with convert the key object into a hashed numeric value which is then used to access a bucket that may contain the key that you are looking for. Then the equality operator is then used to check if that bucket actually contains the value you are looking for.
If this is the case, then it would be conceivable that a
and A
could be in different buckets, thus allowing the two items to be added without conflict, or checked upon and not finding the other. I have to assume that this is not the case as this would break a lot of code out there, so what is really happening here? What's happening behind the curtain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在.NET中,两个对象“相等”(并用作哈希键)必须生成相同的哈希代码。因此,如果您创建具有不敏感比较的字典,则
a
和a
肯定会映射到相同的“桶”。发生这种情况的地方是stringcomparer.currentcurentulturedeorecase
返回的对象,该对象以保留平等关系的方式生成哈希代码。请注意,两个不是不是等于生成相同哈希代码的对象,但是
dictionary
类首次检查哈希码,以查看两个键是否可以 /em>是相等的,如果可以的话,请检查它们是否相等。In .NET, two objects that are "equal" (and used as hashing keys) must generate the same hash code. So if you create a dictionary with a case-insensitive comparer, then
a
andA
will definitely map to the same "bucket". Where that happens is in the object thatStringComparer.CurrentCultureIgnoreCase
returns, which generates hash codes in such a way that the equality relationship is preserved.Note that it's possible for two objects that are not equal to generate the same hash code, but the
Dictionary
class first checks hash codes to see if two keys could be equal, and if they can, then checks to see if they are equal.