internal static void Set(TList list, long index, TElement value)
 {
     if (list == null)
     {
         throw new ArgumentNullException("list");
     }
     if (index < 0L)
     {
         throw new IndexOutOfRangeException();
     }
     if (value == null)
     {
         throw new ArgumentNullException();
     }
     list.InvokeInMonitorLock(() =>
     {
         if (index < list.Count)
         {
             TElement previousNode = null;
             TElement currentNode  = list._firstNode;
             for (long i = 0; i < index; i++)
             {
                 if (currentNode == null)
                 {
                     break;
                 }
                 previousNode = currentNode;
                 currentNode  = currentNode._nextNode;
             }
             if (currentNode != null)
             {
                 value.InvokeInMonitorLock(() =>
                 {
                     if (value._container != null)
                     {
                         if (ReferenceEquals(value._container, list))
                         {
                             if (ReferenceEquals(value, currentNode))
                             {
                                 return;
                             }
                             throw new ArgumentOutOfRangeException("value", ErrorMessage_ItemExistsInCurrent);
                         }
                         throw new ArgumentOutOfRangeException("value", ErrorMessage_ItemExistsInCurrent);
                     }
                     list.InvokeInChange(() =>
                     {
                         RaiseReplacingItem(list, currentNode, value, index);
                         value._nextNode = currentNode._nextNode;
                         if (previousNode == null)
                         {
                             list._firstNode = value;
                         }
                         else
                         {
                             previousNode._nextNode = value;
                         }
                         if (value._nextNode == null)
                         {
                             list._lastNode = value;
                         }
                         value._container       = list;
                         currentNode._container = null;
                     });
                 });
                 RaiseItemReplaced(list, currentNode, value, index);
                 return;
             }
         }
         throw new IndexOutOfRangeException();
     });
 }
 internal static void Insert(TList list, long index, TElement item)
 {
     if (list == null)
     {
         throw new ArgumentNullException("list");
     }
     if (index < 0L)
     {
         throw new ArgumentOutOfRangeException("index");
     }
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     list.InvokeInMonitorLock(() =>
     {
         if (index > list.Count)
         {
             throw new ArgumentOutOfRangeException("index");
         }
         item.InvokeInMonitorLock(() =>
         {
             ValidateItemOrphaned(list, item);
             list.InvokeInChange(() =>
             {
                 RaiseAddingItem(list, item, index);
                 if (index == list.Count)
                 {
                     if (index == 0L)
                     {
                         list._firstNode = item;
                         list.Count      = 1L;
                     }
                     else
                     {
                         list._lastNode._nextNode = item;
                         list.Count++;
                     }
                     list._lastNode = item;
                 }
                 else if (index == 0L)
                 {
                     item._nextNode  = list._firstNode;
                     list._firstNode = item;
                 }
                 else if (index == list.Count - 1)
                 {
                     list._lastNode._nextNode = item;
                     list._lastNode           = item;
                 }
                 else
                 {
                     TElement previousItem = list._firstNode;
                     for (long i = 1L; i < index; i++)
                     {
                         if ((previousItem = previousItem._nextNode) == null)
                         {
                             throw new ArgumentOutOfRangeException("index");
                         }
                     }
                     item._nextNode         = previousItem._nextNode;
                     previousItem._nextNode = item;
                 }
                 item._container = list;
             });
         });
         RaiseItemAdded(list, item, index);
     });
 }