gpt4 book ai didi

c# - C# 中带有字符串键类型的不区分大小写的字典

转载 作者:IT王子 更新时间:2023-10-29 03:33:03 29 4
gpt4 key购买 nike

如果我有一个 Dictionary<String,...>是否可以制作类似 ContainsKey 的方法不区分大小写?

这似乎是相关的,但我没有正确理解它:c# Dictionary: making the Key case-insensitive through declarations

最佳答案

This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations

确实有关系。解决方案是告诉字典实例不要使用标准的字符串比较方法(区分大小写),而是使用不区分大小写的方法。这是使用 appropriate constructor 完成的:

var dict = new Dictionary<string, YourClass>(
StringComparer.InvariantCultureIgnoreCase);

构造函数需要一个 IEqualityComparer它告诉字典如何比较键。

StringComparer.InvariantCultureIgnoreCase为您提供一个 IEqualityComparer 实例,它以不区分大小写的方式比较字符串。

关于c# - C# 中带有字符串键类型的不区分大小写的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988643/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com