views:

150

answers:

1

Hi

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);
        }


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

Main is static so IsEven must be static too.

pb
<grin>... many thanks pb.
Dave