Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以lambda形式将谓词传递给谓词_C# - Fatal编程技术网

C# 以lambda形式将谓词传递给谓词

C# 以lambda形式将谓词传递给谓词,c#,C#,我在否定一个谓词。我是用一种方法做的。但我正在努力研究lamba表达式的语法。在lambda怎么做 static void Main(string[] args) { var words = new List<string> { "falcon", "wood", "tree", "rock", "cloud", "rain" }; Predicate<string> hasFourChars = word => word.Length

我在否定一个谓词。我是用一种方法做的。但我正在努力研究lamba表达式的语法。在lambda怎么做

static void Main(string[] args)
{
    var words = new List<string> { "falcon", "wood", "tree",
      "rock", "cloud", "rain" };

    Predicate<string> hasFourChars = word => word.Length == 4;
    // Predicate<bool> Negate = Predicate<string> fun => word => !fun(word);

    var words2 = words.FindAll(Negate(hasFourChars));

    Console.WriteLine(string.Join(',', words2));
}

static Predicate<T> Negate<T>(Predicate<T> predicate) 
{
   return x => !predicate(x);
}
static void Main(字符串[]args)
{
var words=新列表{“猎鹰”、“木头”、“树”,
“岩石”、“云”、“雨”};
谓词hasFourChars=word=>word.Length==4;
//谓词否定=谓词fun=>word=>!fun(word);
var words2=words.FindAll(否定(hasFourChars));
Console.WriteLine(string.Join(',',words2));
}
静态谓词否定(谓词谓词)
{
返回x=>!谓词(x);
}

您需要使用下一种方法:

Predicate<string> hasFourChars = word => word.Length == 4;
Predicate<string> negate = word => !hasFourChars(word);

您需要使用下一种方法:

Predicate<string> hasFourChars = word => word.Length == 4;
Predicate<string> negate = word => !hasFourChars(word);