C# 谓词不工作的委托

C# 谓词不工作的委托,c#,lambda,delegates,C#,Lambda,Delegates,我正在学习delagate和lambda的表情。我写了一些测试代码,使用Count(),一些调用有效,但最后一个不起作用,我不知道为什么。请帮我解释一下。多谢各位 public static int Count<T>(T[] arr, Predicate<T> condition) { int counter = 0; for (int i = 0; i < arr.Length; i++) if (condition(arr[i])

我正在学习delagate和lambda的表情。我写了一些测试代码,使用Count(),一些调用有效,但最后一个不起作用,我不知道为什么。请帮我解释一下。多谢各位

public static int Count<T>(T[] arr, Predicate<T> condition)
{
    int counter = 0;
    for (int i = 0; i < arr.Length; i++)
        if (condition(arr[i]))
            counter++;
    return counter;
}

delegate Boolean IsOdds<T>(T x);
delegate bool IsEven(int x);

private void button1_Click(object sender, EventArgs e)
{
    IsOdds<int> isOdds = i => i % 2 == 1;//must be <int>, not <T>
    IsEven isEven = i => i % 2 == 0;            

    Action<int> a = x => this.Text = (x*3).ToString();
    a(3);

    Predicate<int> f = delegate(int x) { return x % 2 == 1; };
    Predicate<int> ff = x => x % 2 == 1;

    MessageBox.Show("lambada " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, x => x % 2 == 1).ToString());
    MessageBox.Show("f " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, f).ToString());
    MessageBox.Show("ff " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, ff).ToString());
    MessageBox.Show("delegate " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, delegate(int x) { return x % 2 == 1; }).ToString());
    MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isOdds(int x)).ToString());  //this is wrong
    MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isEven(int x)).ToString());  //this is wrong
    return;
}
公共静态整数计数(T[]arr,谓词条件)
{
int计数器=0;
对于(int i=0;ii%2==1;//必须是,而不是
IsEven IsEven=i=>i%2==0;
动作a=x=>this.Text=(x*3.ToString();
a(3);
谓词f=delegate(intx){返回x%2==1;};
谓词ff=x=>x%2==1;
Show(“lambada”+Count(新的int[]{1,2,3,4,5},x=>x%2==1.ToString());
Show(“f”+Count(新的int[]{1,2,3,4,5},f).ToString());
Show(“ff”+Count(新的int[]{1,2,3,4,5},ff.ToString());
Show(“委托”+计数(新的int[]{1,2,3,4,5},委托(int x){return x%2==1;}).ToString());
Show(Count(new int[]{1,2,3,4,5},isOdds(int x)).ToString();//这是错误的
Show(Count(new int[]{1,2,3,4,5},isEven(int x)).ToString();//这是错误的
返回;
}
计数(新的int[]{1,2,3,4,5},isOdds(intx));
这是无效的。isOdds返回布尔值并接受整数及其必像
谓词
。但是你不能直接发送,因为它们的类型不同。你必须使用另一个lambda

Count<int>(new int[] { 1, 2, 3, 4, 5 }, x => isOdds(x)); // x => isOdds(x) is now type of Predicate<int>
Count(新的int[]{1,2,3,4,5},x=>isOdds(x));//x=>isOdds(x)现在是谓词的类型
对我来说也是一样

还有另一种方法。您可以创建新的谓词

Count<int>(new int[] { 1, 2, 3, 4, 5 }, new Predicate<int>(isOdds)); 
Count(新int[]{1,2,3,4,5},新谓词(isOdds));

基本上
委托布尔型IsOdds(tx)
等价于
谓词
委托布尔型IsEven(intx)
等价于
谓词
因此,您可以省略自己的代理声明,并编写如下内容:

private void button1_Click(object sender, EventArgs e)
    {
        Predicate<int> isOdds = i => i % 2 == 1;
        Predicate<int> isEven = i => i % 2 == 0;

        Action<int> a = x => this.Text = (x * 3).ToString();
        a(3);

        Predicate<int> f = delegate (int x) { return x % 2 == 1; };
        Predicate<int> ff = x => x % 2 == 1;

        MessageBox.Show("lambada " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, x => x % 2 == 1).ToString());
        MessageBox.Show("f " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, f).ToString());
        MessageBox.Show("ff " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, ff).ToString());
        MessageBox.Show("delegate " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, delegate (int x) { return x % 2 == 1; }).ToString());
        MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isOdds).ToString());
        MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isEven).ToString());
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
谓词isOdds=i=>i%2==1;
谓词isEven=i=>i%2==0;
动作a=x=>this.Text=(x*3.ToString();
a(3);
谓词f=delegate(intx){返回x%2==1;};
谓词ff=x=>x%2==1;
Show(“lambada”+Count(新的int[]{1,2,3,4,5},x=>x%2==1.ToString());
Show(“f”+Count(新的int[]{1,2,3,4,5},f).ToString());
Show(“ff”+Count(新的int[]{1,2,3,4,5},ff.ToString());
Show(“委托”+计数(新的int[]{1,2,3,4,5},委托(int x){return x%2==1;}).ToString());
Show(Count(新的int[]{1,2,3,4,5},isOdds.ToString());
Show(Count(新的int[]{1,2,3,4,5},isEven.ToString());
}
private void button1_Click(object sender, EventArgs e)
    {
        Predicate<int> isOdds = i => i % 2 == 1;
        Predicate<int> isEven = i => i % 2 == 0;

        Action<int> a = x => this.Text = (x * 3).ToString();
        a(3);

        Predicate<int> f = delegate (int x) { return x % 2 == 1; };
        Predicate<int> ff = x => x % 2 == 1;

        MessageBox.Show("lambada " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, x => x % 2 == 1).ToString());
        MessageBox.Show("f " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, f).ToString());
        MessageBox.Show("ff " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, ff).ToString());
        MessageBox.Show("delegate " + Count<int>(new int[] { 1, 2, 3, 4, 5 }, delegate (int x) { return x % 2 == 1; }).ToString());
        MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isOdds).ToString());
        MessageBox.Show(Count<int>(new int[] { 1, 2, 3, 4, 5 }, isEven).ToString());
    }