Predicate<t> and Func<string> as T</string></t>



  • There's a list visiting Func pipeline string constituent area to call List.RemoveAll(). There's a need to hand over Predicate Verification Tstate. Only defined functions need to be removed. MSDN has seen an example from Point, but it is not possible to redo the same as Func. What's the problem?

        List<Func<string>> _list;
        public MainWindow()
        {
            _list = new List<Func<string>>();
            _list.Add(this.test);
            _list.Add(this.test2);
            _list.Add(this.test);
            _list.Add(this.test2);
            _list.Add(this.test);
    
        Predicate&lt;Func&lt;string&gt;&gt; pre = ValidateFunc(test2,nameof(test2));// тут функция и подчеркивается с ошибкой
    }
    
    private static bool ValidateFunc(Func&lt;string&gt; obj,string targetName)
    {
        return targetName == nameof(obj);
    }
    
    string test()
    {
        textBox.Text += " 2";
        return "";
    }
    string test2()
    {
        textBox.Text += " 3";
        return "";
    }
    

    Mistake: It's impossible to convert the type of "bool" into "System.Predicate System.Functure string constituent"

    If the MSDN without parameters is such a mistake: There is no overloaded method for "ValidateFunc" which corresponds to the delegate "Predicate Func spinning tie"



  • I'm:

    Predicate<Func<string>> pre = item => item == this.test;
    

    In particular, using this predicate as an argument of the method _list.RemoveAllfrom the collection. _list all entry of the method test

    Either so, if instead of referring to the method, you want to use a line with the name of the method:

    Predicate<Func<string>> pre = item => item.Method.Name == "test";
    

    The purpose of your method ValidateFunc I couldn't understand. You compare the line in it. targetName c nameof(obj) (i mean, with a string station "obj")




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2