/// <summary>
 /// Iterates over all elements in <paramref name="list"/> and calls
 /// <paramref name="handler"/>. If <paramref name="list"/> implements
 /// <see cref="ThreadSafe.IList{T}"/>, only thread safe methods are called.
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="list">A list</param>
 /// <param name="handler">Called for each element</param>
 public static void Iterate <T>(this IList <T> list, ListIterateDelegate <T> handler)
 {
     list.Iterate(0, -1, false, handler);
 }
 /// <summary>
 /// Iterates over all elements in <paramref name="list"/> in the reverse order and calls
 /// <paramref name="handler"/>. If <paramref name="list"/> implements
 /// <see cref="ThreadSafe.IList{T}"/>, only thread safe methods are called.
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="list">A list</param>
 /// <param name="handler">Called for each element</param>
 public static void IterateReverse <T>(this IList <T> list, ListIterateDelegate <T> handler)
 {
     list.Iterate(0, -1, true, handler);
 }
        /// <summary>
        /// Iterates over elements in <paramref name="list"/> and calls <paramref name="handler"/>.
        /// If <paramref name="list"/> implements <see cref="ThreadSafe.IList{T}"/>, only thread safe
        /// methods are called.
        /// </summary>
        /// <typeparam name="T">Type to store in list</typeparam>
        /// <param name="list">A list</param>
        /// <param name="handler">Called for each element</param>
        /// <param name="startIndex">Start index</param>
        /// <param name="endIndex">End index. <c>-1</c> means <c>Count_NoLock</c></param>
        /// <param name="reverseOrder"><c>true</c> if we should iterate in the reverse order</param>
        public static void Iterate <T>(this IList <T> list, int startIndex, int endIndex, bool reverseOrder, ListIterateDelegate <T> handler)
        {
#if THREAD_SAFE
            var tsList = list as ThreadSafe.IList <T>;
            if (tsList != null)
            {
                tsList.Iterate(startIndex, endIndex, reverseOrder, (tsList2, index, value) => handler(tsList2, index, value));
            }
            else
            {
#endif
            if (reverseOrder)
            {
                int i = (endIndex < 0 ? list.Count : endIndex) - 1;
                for (; i >= startIndex; i--)
                {
                    if (!handler(list, i, list[i]))
                    {
                        break;
                    }
                }
            }
            else
            {
                // Count property can change so check it each time in the loop
                for (int i = startIndex; i < (endIndex < 0 ? list.Count : endIndex); i++)
                {
                    if (!handler(list, i, list[i]))
                    {
                        break;
                    }
                }
            }
#if THREAD_SAFE
        }
#endif
        }