Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Some Extensions for IEnumerable

0.00/5 (No votes)
22 Dec 2010 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here