【问题标题】:Creating a Custom Predicate创建自定义谓词
【发布时间】:2019-01-07 10:22:35
【问题描述】:

我是 Lambda 和代表的新手。我认为我的问题可能不是一个好问题,但我正在尝试编写一个类似于内置谓词的简单自定义谓词。

所以我要分享我的代码:请与我分享我将在哪里犯错误:

内置谓词代码示例:

namespace Built_In_Predicate
{
    class Program
    {
        static void Main(string[] args)
        {

            List<string> _ListOfPlayers = new List<string>()
            {
                "James Anderson",
                "Broad",
                "foo"
            };

            // Method 1. Predicate and Anonymous function.

            Predicate<string> _Predicate = delegate (string someString) { return someString.Length == 3; };

            string result = _ListOfPlayers.Find(_Predicate);

            Console.WriteLine("Result : {0}", result);
        }
    }
}

尝试创建自定义谓词(代码):

namespace CustomPredicate
{
    class Program
    {
        // Delegate (Takes some string as a Input and return a Boolean.)
        public delegate bool CustomPredicate(string someString);

        static void Main(string[] args)
        {

            List<string> _ListOfPlayers = new List<string>()
            {
                "James Anderson",
                "Broad",
                "foo"
            };

            //  Instance of CustomPredicate.
            CustomPredicate customPredicate = delegate (string someString) { return someString.Length == 3; };

            string result = _ListOfPlayers.Find(customPredicate); // its error.
        }
    }
}

我们将不胜感激。

【问题讨论】:

  • 为什么要使用自定义委托,为什么希望它能够工作?
  • @Sweeper 我只是在练习代表......这就是为什么
  • 您不能将自定义谓词 type 传递给 List&lt;T&gt;.Find。但“自定义谓词”仅表示包含您自己的逻辑的 Predicate&lt;T&gt; 类型的实例。

标签: c# lambda delegates anonymous-function predicate


【解决方案1】:

即使具有相同的签名,委托也不能隐式相互转换。

Find 需要 System.Predicate&lt;T&gt;,所以你必须给它一个 System.Predicate&lt;T&gt;

如果你想使用自己的CustomPredicate,你可以编写自己的Find方法。

还有一些方法可以在对Find 的调用中使用您的customPredicate 变量:

_ListOfPlayers.Find(new Predicate<string>(customPredicate));
_ListOfPlayers.Find(customPredicate.Invoke);

【讨论】:

  • 谢谢)-这就是我想要的......customPreicate.Invoke为我工作......
  • @Rehan.S 请注意,我永远不会在生产代码中编写类似的东西。重新发明Predicate 委托并以这种方式使用它根本没有意义。事实上,我宁愿在这里使用 lambda 表达式:_ListOfPlayers.Find(x =&gt; x.Length == 3)
  • 非常感谢您的有用建议...但在我这边,我不知道如何调用我写的customPredicate...任何您的回答都可以实现... .
【解决方案2】:

你不能用别的东西作为谓词类型来调用 Find。但是如果你想要自己的委托,你可以调用 FirstOrDefault (System.Linq) 然后使用它。

    private delegate bool CustomPredicate (string t);

    static void Main(string[] args)
    {
        List<string> _ListOfPlayers = new List<string>()
        {
            "James Anderson",
            "Broad",
            "foo"
        };

        // Method 1. Predicate and Anonymous function.

        CustomPredicate _Predicate = delegate (string someString) { return    someString.Length == 3; };

        string result = _ListOfPlayers.FirstOrDefault(x => _Predicate(x));
        Console.WriteLine("Result : {0}", result);
        Console.ReadLine();
    }

【讨论】:

  • 不... FirstOrDefault 返回IEnumerable 这就是我不想要的...
  • 没有。它返回字符串(或空字符串)。我猜你的意思可能是“.Where(..)”?
  • 请看情报...你看到了吗...它回馈IEnumerable
  • Intellisense 告诉我:字符串 IEnumerable.FirstOrDefault。第一个字符串是结果。 IEnumerable 是要应用的类型。如果您检查 Intellisense 的 .Where,您将看到 IEnumerable IEnumerable.Where
猜你喜欢
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多