internal static long IndexOf(TList list, TElement item)
 {
     if (list == null)
     {
         throw new ArgumentNullException("list");
     }
     return(list.GetInMonitorLock(() =>
     {
         if (item != null && item.GetInMonitorLock(() => item._container != null && ReferenceEquals(item._container, list)))
         {
             if (item._nextNode == null)
             {
                 return list.Count - 1;
             }
             long index = -1L;
             for (TElement node = list._firstNode; node != null; node = node._nextNode)
             {
                 index++;
                 if (ReferenceEquals(node, item))
                 {
                     return index;
                 }
             }
         }
         return -1L;
     }));
 }
 internal static TElement Get(TList list, long index)
 {
     if (index < 0L)
     {
         throw new IndexOutOfRangeException();
     }
     return(list.GetInMonitorLock(() =>
     {
         if (index < list.Count)
         {
             TElement node = list._firstNode;
             for (long i = 0; i < index; i++)
             {
                 if (node == null)
                 {
                     break;
                 }
                 node = node._nextNode;
             }
             if (node != null)
             {
                 return node;
             }
         }
         throw new IndexOutOfRangeException();
     }));
 }
                /// <summary>
                /// Advances the enumerator to the next <typeparamref name="TElement" /> of the <typeparamref name="TList" />.
                /// </summary>
                /// <returns><c>true</c> if the enumerator was successfully advanced to the next <typeparamref name="TElement" /> ;
                /// otherwise, <c>false</c> if the enumerator has passed the end of the typeparamref name="TList" />.</returns>
                /// <exception cref="InvalidOperationException">The <typeparamref name="TList" /> was modified after the enumerator was created or last <seealso cref="Reset">.</exception>
                /// <exception cref="ObjectDisposedException">The current enumerator or the <typeparamref name="TList" /> being iterated has been <sealso cref="Dispose" />d.</exception>
                public bool MoveNext()
                {
                    TList list = _list;

                    if (list == null)
                    {
                        throw new ObjectDisposedException(typeof(Enumerator).FullName);
                    }
                    return(list.GetInMonitorLock(() =>
                    {
                        if (_list == null)
                        {
                            throw new ObjectDisposedException(typeof(Enumerator).FullName);
                        }
                        if (_count != list.Count || (_count > 0 && !(ReferenceEquals(_firstNode, list._firstNode) && ReferenceEquals(_lastNode, list._lastNode))))
                        {
                            throw new InvalidOperationException("List items have changed");
                        }
                        if (Index < 0)
                        {
                            if (_firstNode == null)
                            {
                                return false;
                            }
                            Current = _firstNode;
                            Index = 0;
                        }
                        else
                        {
                            TElement node = Current._nextNode;
                            if (node == null)
                            {
                                return false;
                            }
                            Current = node;
                            Index++;
                        }
                        return true;
                    }));
                }
 internal static bool Remove(TList list, TElement item)
 {
     if (list == null)
     {
         throw new ArgumentNullException("list");
     }
     if (item == null)
     {
         return(false);
     }
     return(list.GetInMonitorLock(() =>
     {
         if (list.Count == 0)
         {
             return false;
         }
         if (item.GetInMonitorLock(() => item._container == null || !ReferenceEquals(item._container, list)))
         {
             return false;
         }
         if (ReferenceEquals(list._firstNode, item))
         {
             list.InvokeInChange(() =>
             {
                 RaiseRemovingItem(list, item, 0L);
                 list._firstNode = item._nextNode;
                 if (list._firstNode == null)
                 {
                     list.Count = 0L;
                     list._lastNode = null;
                 }
                 else if (list._firstNode._nextNode == null)
                 {
                     list.Count = 1L;
                     list._lastNode = list._firstNode;
                 }
                 else
                 {
                     list.Count--;
                 }
                 item._container = null;
                 item._nextNode = null;
             });
             RaiseItemRemoved(list, item, 0L);
             return true;
         }
         long index = 0L;
         TElement previousItem = list._firstNode;
         for (TElement node = previousItem._nextNode; node != null; node = node._nextNode)
         {
             index++;
             if (ReferenceEquals(node, item))
             {
                 list.InvokeInChange(() =>
                 {
                     RaiseRemovingItem(list, item, index);
                     previousItem._nextNode = item._nextNode;
                     if (previousItem._nextNode == null)
                     {
                         list._lastNode = previousItem;
                     }
                 });
                 RaiseItemRemoved(list, item, index);
                 return true;
             }
             previousItem = node;
         }
         return false;
     }));
 }