C# C语言中的简单匿名方法#

C# C语言中的简单匿名方法#,c#,delegates,anonymous-methods,C#,Delegates,Anonymous Methods,请参阅下面代码的第二位。。代码不能编译。我正试图找出另一种方法,我明白了 但不是我在网上找到的不使用anon方法的例子,它不编译 使用VS2008。。编译到.NET3.5 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestAnonymousMethods { public class Program { // us

请参阅下面代码的第二位。。代码不能编译。我正试图找出另一种方法,我明白了

但不是我在网上找到的不使用anon方法的例子,它不编译

使用VS2008。。编译到.NET3.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestAnonymousMethods
{
    public class Program
    {
        // using an anon method
        static void Mainx(string[] args)
        {
            int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            int[] evenIntegers = Array.FindAll(_integers,
                                        // this is the anonymous method below
                                       delegate(int integer)
                                       {
                                           return (integer % 2 == 0);
                                       }
                );

            foreach (int integer in _integers) 
                Console.WriteLine(integer);

            foreach (int integer in evenIntegers)
                Console.WriteLine(integer);
        }

        // not using anon method
        static void Main(string[] args)
        {
            int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            int[] evenIntegers = Array.FindAll(_integers, IsEven); // **Compile error here**

            foreach (int integer in _integers)
                Console.WriteLine(integer);

            foreach (int integer in evenIntegers)
                Console.WriteLine(integer);
        }

        public bool IsEven(int integer)
        {
            return (integer % 2 == 0);
        }


    }
}
Main是静态的,所以IsEven也必须是静态的


Main是静态的,因此IsEven也必须是静态的。

作为将来的参考,如果您在问题中包含错误消息,那么如果您遇到编译时错误,它可以帮助很多人。它通常包含有用的信息,可以帮助人们整理代码以备将来参考。如果您在问题中包含错误消息,那么如果您遇到编译时错误,它可以帮助人们很多。它通常包含有用的信息,可以帮助人们梳理代码
public static  bool IsEven(int integer)
{
    return (integer % 2 == 0);
}