Click here to Skip to main content
11,584,572 members (62,405 online)
Click here to Skip to main content

Tagged as

Some Extensions for IEnumerable

, 22 Dec 2010 CPOL 2.7K 2
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

jfriedman
Software Developer (Senior) ASTI Transportation Inc.
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...

v//

You may also be interested in...

Comments and Discussions

 
Generalplease choose your variable name wisely, you wrote, public s... Pin
ring_027-Dec-10 23:19
memberring_027-Dec-10 23:19 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150707.1 | Last Updated 22 Dec 2010
Article Copyright 2010 by jfriedman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid