Full Circle on Action<T>

by Kofi Sarfo 6. June 2009 00:02

Reminded recently of a method for determining Prime Numbers (Sieve of Eratosthenes) and saw some Generic Collection Class action (p. 97 C# In Depth)

            List<int> candidates = new List<int>();

            for (int i = 2; i <= 100; i++)
            {
                candidates.Add(i);
            }
 
            for (int factor = 2; factor <= 10; factor++)
            {
                candidates.RemoveAll(delegate(int x)
                    { return x > factor && x % factor == 0; }
                );
            }
 
            candidates.ForEach(delegate(int prime)
                { Console.WriteLine(prime); }
            );

Bouncing around looking for code samples to see what we're missing in terms of Generics... we spot something. Switch on the Code: The Built-In Generic Delegate Declarations.

Difference between Func<T> and Action<T> is the latter doesn't have a return type. Too obvious to miss? Maybe. Quick search shows difference between new Action() and Lambda at Stack Overflow. Response from author of code sample above. Jon Skeet. 3900 answers on Stack Overflow in 8 months. Or 16 answers a day, every day.

Notes:

MSDN: Lambda Expressions

CodeBetter: Back to Basics - Delegates, Anonymous Methods and Lambda Expressions

Kofi Sarfo modified theme by Mads Kristensen



Content by WIMIRO Technology is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.

Creative Commons License

Powered by BlogEngine.NET 1.5.0.7

About Me

Director, Wimiro Technology
London, United Kingdom

Writes in third person and first person plural; currently commutes to Moorgate.

Kiva Loans

  • Stalamarice Nthomo Kangi

    Stalamarice Nthomo Kangi

    Tailoring

    Requested loan: $375

    Amount raised: $0

    Machakos, Kenya

    To buy fabric

    Loan Now »

  • Jonathan Mututu Kasyoka

    Jonathan Mututu Kasyoka

    Internet Cafe

    Requested loan: $500

    Amount raised: $0

    Machakos, Kenya

    To buy more chairs, tables and printer paper

    Loan Now »

  • Jeanciana Rin

    Jeanciana Rin

    General Store

    Requested loan: $300

    Amount raised: $0

    Katutungan, Wao Lanao Del Sur, Philippines

    To purchase additional inventory and to use as working capital

    Loan Now »

 To see more entrepreneurs »

Kiva Loans