为什么我不能在 C# 中定义不区分大小写的字典?

发布于 2024-08-25 21:25:45 字数 1287 浏览 4 评论 0原文

此 C#/WPF 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestDict28342343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Dictionary<string, string> variableNamesAndValues = 
                new Dictionary<string, string>(StringComparison.InvariantCultureIgnoreCase);

        }
    }
}

给我错误

最佳重载方法匹配 'System.Collections.Generic.Dictionary.Dictionary(System.Collections.Generic.IDictionary)' 有一些无效参数

但是我发现这个代码示例无处不在,例如此处此处

如何定义键不区分大小写的字典?

This C#/WPF code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestDict28342343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Dictionary<string, string> variableNamesAndValues = 
                new Dictionary<string, string>(StringComparison.InvariantCultureIgnoreCase);

        }
    }
}

gives me the error:

The best overloaded method match for
'System.Collections.Generic.Dictionary.Dictionary(System.Collections.Generic.IDictionary)'
has some invalid arguments

Yet I find this code example everywhere such as here and here.

How can I define a Dictionary whose keys are case-insensitve?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

濫情▎り 2024-09-01 21:25:45

您正在尝试使用 StringComparison,这是一个枚举。您应该使用 StringComparer.InvariantCultureIgnoreCase< /a> 相反 - 这是一个返回 StringComparer,它实现 IEqualityComparer 。然后,您最终将调用 Dictionary< ;,> 构造函数重载接受 IEqualityComparer,可用于检查相等性并生成哈希码。

You're trying to using StringComparison, which is an enum. You should be using StringComparer.InvariantCultureIgnoreCase instead - that's a property returning a StringComparer, which implements IEqualityComparer<string>. You'll then end up calling the Dictionary<,> constructor overload accepting an IEqualityComparer<TKey> which it can use to check for equality and generate hash codes.

幸福不弃 2024-09-01 21:25:45

传递 StringComparer.InvariantCultureIgnoreCase。注意 StringComparer 而不是 StringComparison

更一般地,Dictionary 构造函数可以采用 IComparer 类型的参数。正如 Jon 指出的,StringComparison 是一个枚举。但是 StringComparer 提供了一些 IComparer的“固定”实现,而您需要的是后者。

Pass StringComparer.InvariantCultureIgnoreCase. Note StringComparer not StringComparison.

More generally, the Dictionary<TKey, TValue> constructor can take an argument of type IComparer<TKey>. As Jon notes, StringComparison is an enum. But StringComparer provides some "canned" implementation of IComparer<string>, and it's the latter that you need.

木槿暧夏七纪年 2024-09-01 21:25:45

这在我的电脑上有效:

Dictionary<string, string> dic = new Dictionary<string, string>( StringComparer.InvariantCultureIgnoreCase ).

This works on my computer:

Dictionary<string, string> dic = new Dictionary<string, string>( StringComparer.InvariantCultureIgnoreCase ).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文