谓词帮助代表

发布于 2024-08-19 03:37:43 字数 677 浏览 11 评论 0原文

我正在尝试创建一个重载的 Add 方法作为 OrderedDictionary 类的扩展,并希望基于某些柯里化谓词添加键/值。

调用代码如下所示:


OrderedDictionary dict = new OrderedDictionary();

Predicate<int> lessThan5 = i=>; i < 5;
Predicate<string> lenOf2 = s=> s.length == 2;

dict.Add("01","Name", lessThan5 );
dict.Add("02","place", lenOf2);

我创建了一个如下所示的扩展方法:


public static class CollectionExtensions
{
    public static void Add(this OrderedDictionary s, string k, string v, Predicate p)
    {
        if (p)
        {
            d.Add(k, v);
        }
    }
}

但它不起作用,因为我收到编译器错误“无法将谓词转换为布尔值”。

有谁知道我缺少什么?

感谢您的任何帮助。 -基思

I am trying to create an overloaded Add method as an extension to the OrderedDictionary class and would like to add the key/value based on some curried predicate.

The calling code would look like this:


OrderedDictionary dict = new OrderedDictionary();

Predicate<int> lessThan5 = i=>; i < 5;
Predicate<string> lenOf2 = s=> s.length == 2;

dict.Add("01","Name", lessThan5 );
dict.Add("02","place", lenOf2);

I have created an extension method like so:


public static class CollectionExtensions
{
    public static void Add(this OrderedDictionary s, string k, string v, Predicate p)
    {
        if (p)
        {
            d.Add(k, v);
        }
    }
}

But it doesn't work because I get a compiler error reading "cannot convert Predicate to bool".

Does anyone know what I am missing?

Thanks for any help.
-Keith

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

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

发布评论

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

评论(1

暖伴 2024-08-26 03:37:43

问题是您没有评估谓词来检查谓词是否满足。现在,您的问题尚不清楚您是否希望谓词测试键或值。下面检查密钥。您还应该考虑让该方法返回 bool 来指示成功或失败,就像我在这里所做的那样。

static class OrderedDictionaryExtensions {
    public static bool Add(
        this OrderedDictionary dictionary,
        string key,
        string value,
        Predicate<string> predicate
    ) {
        if (dictionary == null) {
            throw new ArgumentNullException("dictionary");
        }
        if (predicate == null) {
            throw new ArgumentNullException("predicate");
        }
        if (predicate(key)) {
            dictionary.Add(key, value);
            return true;
        }
        return false;
    }
}

用法;

// dictionary is OrderedDictionary
dictionary.Add("key", "value", s => s.Length < 5);

实际上,您可以对此进行概括,因为 OrderedDictionary 不是强类型的。

static class OrderedDictionaryExtensions {
    public static bool Add<TKey, TValue>(
        this OrderedDictionary dictionary,
        TKey key,
        TValue value,
        Predicate<TKey> predicate
    ) {
        if (dictionary == null) {
            throw new ArgumentNullException("dictionary");
        }
        if (predicate == null) {
            throw new ArgumentNullException("predicate");
        }
        if (predicate(key)) {
            dictionary.Add(key, value);
            return true;
        }
        return false;
    }
}

用法:

// dictionary is OrderedDictionary
dictionary.Add(17, "Hello, world!", i => i % 2 != 0);

The issue is that you are not evaluating your predicate to check and see whether or not the predicate is satisfied. Now, it's not clear from your question if you want the predicate to test the key or the value. The following checks the key. You should also consider having the method return bool indicating success or failure as I've done here.

static class OrderedDictionaryExtensions {
    public static bool Add(
        this OrderedDictionary dictionary,
        string key,
        string value,
        Predicate<string> predicate
    ) {
        if (dictionary == null) {
            throw new ArgumentNullException("dictionary");
        }
        if (predicate == null) {
            throw new ArgumentNullException("predicate");
        }
        if (predicate(key)) {
            dictionary.Add(key, value);
            return true;
        }
        return false;
    }
}

Usage;

// dictionary is OrderedDictionary
dictionary.Add("key", "value", s => s.Length < 5);

You can actually generalize this a bit since OrderedDictionary is not strongly-typed.

static class OrderedDictionaryExtensions {
    public static bool Add<TKey, TValue>(
        this OrderedDictionary dictionary,
        TKey key,
        TValue value,
        Predicate<TKey> predicate
    ) {
        if (dictionary == null) {
            throw new ArgumentNullException("dictionary");
        }
        if (predicate == null) {
            throw new ArgumentNullException("predicate");
        }
        if (predicate(key)) {
            dictionary.Add(key, value);
            return true;
        }
        return false;
    }
}

Usage:

// dictionary is OrderedDictionary
dictionary.Add(17, "Hello, world!", i => i % 2 != 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文