Monday, January 30, 2012

Best Extension Methods: Replace Items in Collection

This extension method replaces an item in a collection that implements the IList interface.
   /// 
        /// This extension method replaces an item in a collection that implements the IList interface.
        /// 
        /// The type of the field that we are manipulating
        /// The input list
        /// The position of the old item
        /// The item we are goint to put in it's place
        /// True in case of a replace, false if failed
        public static bool Replace(this IList thisList, int position, T item)
        {
            if (position > thisList.Count - 1)
                return false;
            // only process if inside the range of this list

            thisList.RemoveAt(position);
            // remove the old item
            thisList.Insert(position, item);
            // insert the new item at its position
            return true;
            // return success
        }

Compiled By: Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates