ISourceCode

Make the frequent cases fast and the rare case correct

C# – Writing an Extension Method

A simple extension method that extends the List class. The extension method accepts a Func delegate. The algorithm is to multiply each element in the list by 2. The Func delegate points to this algorithm method.

using System;
using System.Collections.Generic;

namespace ExtensionMethodFunc
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            for (int i = 1; i <= 10; i++)
                list.Add(i);

            // Approach 1 to call the extension method with the Func delegate
            foreach (int i in list.GetMyList(MultiplyByTwo))
                Console.WriteLine(i);

            // Approach 2, Func delegate explicitly declared pointing to the method
            //Func<int, int> algo = MultiplyByTwo;
            //foreach (int i in list.GetMyList(algo))
            //	Console.WriteLine(i);

            // Approach 3, using an anonymous method, C# 2.0 feature
            //Func<int, int> algo = delegate(int x) { return x * 2; };
            //foreach (int i in list.GetMyList(algo))
            //  Console.WriteLine(i);

            // Approach 4, using lambda expression which is an anonymous function
            // C# 3.0 feature
            //Func<int, int> algo = x => x * 2;
            //foreach (int i in list.GetMyList(algo))
            //	Console.WriteLine(i);

            // Approach 5, using lambda expression without explicitly declaring a func
            //foreach (int i in list.GetMyList(m => m * 2))
            //	Console.WriteLine(i);

            // Approach 6, redundant lambda calling original method, not to be used
            //Func<int, int> algo = x => MultiplyByTwo(x);
            //foreach (int i in list.GetMyList(algo))
            //    Console.WriteLine(i);

            Console.ReadKey();
        }

        private static int MultiplyByTwo(int value)
        {
            return value * 2;
        }
    }

    static class ExtensionMethods
    {
        // Non generic implementation of extension method
        //
        //public static IEnumerable<int> GetMyList(this IEnumerable<int> list, Func<int, int> selector)
        //{
        //	foreach (int item in list)
        //		yield return selector(item);
        //}

        public static IEnumerable<TResult> GetMyList<TSource, TResult>(this IEnumerable<TSource> list, 
            Func<TSource, TResult> selector)
        {
            foreach (TSource item in list)
                yield return selector(item);
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: