Share on reddit
[`google_buzz` not found]
Bookmark this on Digg
Share on StumbleUpon

An extension method that will be making its way into the Generic Extension Methods library is one I want to share tonight.  It’s for projecting an action N times.  Why such a method doesn’t already exist baffles me!

        public static T Project<T>(this T projected, int n, Action<T> action)


            for (int i = 0; i < n; ++i) action(projected);

            return projected;


Usage could not be simpler, any object can be projected to the specified action any number of times.

        public static int Method()


            var value = 1;

            return value.Project(10, i => i*=i);



The projection in this case yields 10^10 as the answer.  Overly simplistic example, but makes the point succinctly.