Generic Extension Methods

Extension methods are used across .NET library more often than not, Let’s see through below example

using System;
using System.Collections.Generic;
using System.Text;
class Program
{        
     static void Main(string[] args)
     {
         IEnumerable list = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
         list.
     }
}

When we dot there are only five methods you can see an images, where are other methods like select, count, sum etc….
See below visual reference

Step1
Food For Thought
First of let me clear all these methods are extension methods of IEnumerable to use these methods we need Enumerable under System.Linq namespace contains all extension methods, See below visual reference of Enumerable class much like utility class
Step2

Just add using System.Linq; will include all necessary extension methods see below , can be seen arrow going bottom against  methods indicating extension methods
Step3

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace LinqToSql
{
    class Program
    {        
        static void Main(string[] args)
        {
            IEnumerable list = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            list.Sum();
        }
    }    
}

Last but not the least below is complete example to create new generic extension method of IENumerable

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace LinqToSql
{
    /// Extension Or Utility Class 
    public static class Enumerable
    {
        /// Syntax For Extenstion Method
        /// static Type MehodeName(this Type)
        public static IEnumerable NumberFilter(this IEnumerable list, Predicate condition)
        {
            foreach (var item in list)
            {
                if (condition(item))
                {
                    yield return item;
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable list = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            /// NumberFilter Available Without Doing Much
            IEnumerable filterEven = list.NumberFilter(x => x % 2 == 0);
            foreach (var item in filterEven)
            {
                Console.WriteLine("Even Number:{0}", item);
            }
            IEnumerable filterOld = list.NumberFilter(x => x % 2 != 0);
            foreach (var item in filterEven)
            {
                Console.WriteLine("Odd Number:{0}", item);
            }
        }
    }
}

Visual Reference For New Generic Extension Method

Step4
Advertisements

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