Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Some Extensions for IEnumerable

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
22 Dec 2010CPOL 8.9K   2   1
In my opinion, creating the new interface seems to be an unnecessary part of this article...Extensions Methods for IEnumerable is the topic of the said article…which is provided by the existing system.Building a new interface which is not part of the native system is not actually...
In my opinion, creating the new interface seems to be an unnecessary part of this article...

Extensions Methods for IEnumerable is the topic of the said article…which is provided by the existing system.

Building a new interface which is not part of the native system is not actually required and is a nice extension of said article, however in my humble opinion is an unnecessary burden to the reader to digest.

Consider the following alternative solutions used by an open source project:

http://dnpextensions.codeplex.com/

It required no new interfaces and provided the same type of logic without the interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tools.Utilities
{
    public static class EnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> @enum, Action<T> mapFunction)
        {
            if (null == @enum) throw new ArgumentNullException("@enum");
            if (mapFunction == null) throw new ArgumentNullException("mapFunction");
            //@enum.ToList().ForEach(mapFunction);
            foreach (T item in @enum) mapFunction(item);
        }

        public static bool IsNullOrEmpty<T>(this IEnumerable<T> iEnumerable)
        {
            return iEnumerable == null || !iEnumerable.Any();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tools.Utilities
{
    public static class ListExtensions
    {
        /// <summary>
        /// Fast version of the RemoveAt function. Overwrites the element at the specified index
        /// with the last element in the list, then removes the last element, thus lowering the
        /// inherent O(n) cost to O(1). Intended to be used on *unordered* lists only.
        /// </summary>
        /// <param name="_list">IList</param>
        /// <param name="_index">Index of the element to be removed.</param>
        public static void RemoveAtFast<T>(this IList<T> _list, int _index)
        {
            if (_index < 0) return;

            //get the amount of items in the list once
            int count = _list.Count - 1;

            if (_index > count) return;

            //copy the last item to the index being removed
            _list[_index] = _list[count];
            ///still calling remove at because the old item was copied to the removed index
            ///and we need the list to reflect the remove operation
            _list.RemoveAt(count);
            ///this will remove the last item which will not allow array.copy to be called resulting in a faster removal
            ///array.copy is not called  because the element being removed it at the end of the array.
        }

        /// <summary>
        /// Performs the specified action on each element in the list while providing the index to the action
        /// </summary>
        /// <typeparam name="T">The Type of Elements in the IList</typeparam>
        /// <param name="_list">The IList to perform the Action on</param>
        /// <param name="_action">The Action to perform on the element of the IList</param>
        public static void ForEach<T>(this IList<T> _list, Action<T> _action)
        {
            for (int index = 0, end = _list.Count; index < end; index++)
            {
                _action(_list[index]);
            }
        }

        /// <summary>
        /// Performs the specified action on each element in the list while providing the index to the action
        /// </summary>
        /// <typeparam name="T">The Type of Elements in the IList</typeparam>
        /// <param name="_list">The IList to perform the Action on</param>
        /// <param name="_action">The Action to perform on the element of the IList</param>
        public static void ForEachWithIndex<T>(this IList<T> _list, Action<T, int> _action)
        {
            for (int index = 0, end = _list.Count; index < end; index++)
            {
                _action(_list[index], index);
            }
        }

        /// <summary>
        /// Performs the specified action on each element in the list while providing the index and the origional list to the action
        /// </summary>
        /// <typeparam name="T">The Type of Elements in the IList</typeparam>
        /// <param name="_list">The IList to perform the Action on</param>
        /// <param name="_action">The Action to perform on the element of the IList</param>
        public static void ForEachWithIndex<T>(this IList<T> _list, Action<IList<T>, T, int> _action)
        {
            for (int index = 0, end = _list.Count; index < end; index++)
            {
                _action(_list, _list[index], index);
            }
        }
    }
}


Otherwise, you really didn’t do much but cite the MSDN article which in my opinion seems to be more informative and direct than this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...
мала ка на хари, Trahentes ex exsilium

Comments and Discussions

 
Generalplease choose your variable name wisely, you wrote, public s... Pin
ring_027-Dec-10 23:19
ring_027-Dec-10 23:19 
please choose your variable name wisely,
you wrote,
public static void ForEach<t>(this IEnumerable<t> @enum, Action<t> mapFunction)
but my concern is with the variable mapFunction which is a type of delegate Action<t>, actually mapfunction means a function that takes one type and transform to another type.
For Example,
Select operator is map function. Which is defined like Func<tsource,treturn>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.