0

See the second bit of code below.. code doesn't compile. Am trying to figure out anon methods, and I get it..

But not the example of not using anon methods which I found on the web, which doesn't compile

Using VS2008.. compiling to .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);
        }


    }
}
Thorarin
  • 47,289
  • 11
  • 75
  • 111
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
  • 3
    For future reference, it can help people alot if you are getting a compile-time error if you include the error message along with your question. It usually contains useful information that can save people combing through your code – Matthew Scharley Jul 27 '09 at 05:05

1 Answers1

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

Main is static so IsEven must be static too.

Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54
Paul van Brenk
  • 7,450
  • 2
  • 33
  • 38