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

Comments are closed

Kiva Loans

  • Makenzi Makau

    Makenzi Makau

    Grocery Store

    Requested loan: $200

    Amount raised: $0

    , Kenya

    To buy cabbages and oranges to sell.

    Loan Now »

  • Andrew Katushabe

    Andrew Katushabe

    Pub

    Requested loan: $875

    Amount raised: $0

    Mubende, Uganda

    to buy more drinks such as soda and beer for resale.

    Loan Now »

  • Jessica Joguilon

    Jessica Joguilon

    Pigs

    Requested loan: $350

    Amount raised: $0

    Valladolid, Negros Occidental, Philippines

    to buy feeds and vitamins for her pigs.

    Loan Now »

To see more entrepreneurs »

Make a loan to an entrepreneur across the globe for as little as $25. Kiva is the world's first online lending platform connecting online lenders to entrepreneurs across the globe.