C# 以lambda形式将谓词传递给谓词
我在否定一个谓词。我是用一种方法做的。但我正在努力研究lamba表达式的语法。在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
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);