title: C# LinkedListNode reference after calling Remove on LinkedList
body: <p><strong>My question:</strong> Hi, does method- LinkedList.Remove(LinkedListNode n) changing references of others elements in LinkedList?</p>
  
  <p><em>Application is little bit complex so atleast I will try to explain how I got here, but maybe It will be confusing</em></p>
  
  <blockquote>
    <p>I have a program which storing references(LinkedListNodes from
    LinkedList) into another iterable class. And in some point,
    application starts deleting these LinkedListNodes with
    method-Remove(LinkedListNode node) and also delete this element from
    my class which stores these references. It runs good for a while, but
    in some point It will loose one of the reference in my class and I get
    null reference(in myNode) when i want to call
    LinkeList.AddAfter(myNode, value) with error: “The LinkedList node
    does not belong to current LinkedList”.</p>
  </blockquote>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I was translating notes, etc...
  So I am using BinarySearchTree for quick search, LinkedList for normal iterating and QUEUE for deleting old elements.</p>
  
  <p>This is insert in my Tree class:</p>
  
  <pre><code> public Node Insert(DictionaryPair dictionaryPair, LinkedList&lt;DictionaryPair&gt; dictionary)
      {
          Node currentNode = nodeRoot;
          while (true)
          {
              if (currentNode == null)    //if i inserting 1st element
              {
                  nodeRoot = new Node();   //creating node(root)
                  dictionary.AddFirst(dictionaryPair);    //inserting into Linked list on 1st place
                  nodeRoot.dictionaryNode = dictionary.First;  //and taking a reference
                  return nodeRoot;  //end while
              }
              else if (currentNode.dictionaryNode.Value.CompareTo(dictionaryPair) &gt;= 0)
              {   //sending element into left
                  if (currentNode.left == null)   // and is empty
                  {
                      currentNode.left = new Node();  //creating new node
                      currentNode.left.myParent = currentNode;    //reference to parent
                      currentNode.left.dictionaryNode = dictionary.AddBefore(currentNode.dictionaryNode, dictionaryPair);
                      //and inserting into dictionary (Before) current node and save the refence on it to left
                      return currentNode.left;  //end while
                  }
                  else
                  {   //or shift to left
                      currentNode = currentNode.left;
                  }
              }
              else
              {   //sending element into right
                  if (currentNode.right == null)   // is null
                  {
                      currentNode.right = new Node();  //create new node
                      currentNode.right.myParent = currentNode;   //reference on parent
                      currentNode.right.dictionaryNode = dictionary.AddAfter(currentNode.dictionaryNode, dictionaryPair);
                      //and insert into dictionary (After) current node and save  the refence on it to right
                      return currentNode.right;  //endwhile
                  }
                  else
                  {   //or shift to right side
                      currentNode = currentNode.right;
                  }
              }
          }            
      }
  </code></pre>
  
  <p>class Node:</p>
  
  <pre><code>    public LinkedListNode&lt;DictionaryPair&gt; dictionaryNode;
      public Node left;
      public Node right;
      public Node myParent;
  </code></pre>
  
  <p>I can call delete method on my Node: </p>
  
  <pre><code>public LinkedListNode&lt;DictionaryPair&gt; DeleteMe()
      { 
          LinkedListNode&lt;DictionaryPair&gt; deletedNode = this.dictionaryNode;
          if (this.left == null &amp;&amp; this.right == null)
          {   //Delete leaf
              if(myParent.left == this)
              {
                  myParent.left = null;
              }
              else // else if(myParent.right == this)
              {
                  myParent.right = null;
              }
          }
          else if (this.left != null &amp;&amp; this.right == null)
          {
              this.right = this.left.right;
              this.dictionaryNode = this.left.dictionaryNode;
              this.left = this.left.left;               
  
          }
          else if (this.left == null &amp;&amp; this.right != null)
          {
              this.left = this.right.left;                
              this.dictionaryNode = this.right.dictionaryNode;
              this.right = this.right.right;
          }
          else
          {   //on left and right are tries
              Node currentNode = this.left; //throught the left side
              bool oneCycle = false;  //possibility of not iterating once thought the left side into the right (so it would be left)
              while (currentNode.right != null)
              {    //searching element most to the right
                  currentNode = currentNode.right;
                  oneCycle = true;
              }
              if (currentNode.left == null)
              {   //i am leaf
                  if (oneCycle)
                  {
                      currentNode.myParent.right = null;   //deleting refence on me
                      this.dictionaryNode = currentNode.dictionaryNode;   //and change a value
                  }
                  else
                  {
                      currentNode.myParent.left = null;   //deleting refence on me
                      this.dictionaryNode = currentNode.dictionaryNode;   //and change a value
                  }
              }
              else
              {   //im not leaf
                  if (oneCycle)
                  {
                      currentNode.myParent.right = currentNode.left;  //change refence on my tree
                      this.dictionaryNode = currentNode.dictionaryNode;   //and change value
                  }
                  else
                  {
                      currentNode.myParent.left = currentNode.left;  //change refence on my tree
                      this.dictionaryNode = currentNode.dictionaryNode;   //and change value
                  }
              }             
          }
          return deletedNode;
      }
  </code></pre>
  
  <p>This is my main class for working with the arrays MyDictionary, whichs have</p>
  
  <pre><code>    private LinkedList&lt;DictionaryPair&gt; data;    //for iterating search
      private Tree binarySearchTree;  //for quick search
      private Queue&lt;Node&gt; queue;   //references on added nodes 
      //in binarySearchTree... they are ready to be deleted
      private int maxCount;
      private bool maxCountReached;
  </code></pre>
  
  <p>When I am inserting into MyDictionary, I calling this method</p>
  
  <pre><code>public void Insert(DictionaryPair input)
      {
          if (!maxCountReached)
          {
              if (queue.Count() &gt;= maxCount)
              {
                  maxCountReached = true;
              }
          }
          if (maxCountReached)
          {
              data.Remove(queue.Dequeue().DeleteMe());
          }
          queue.Enqueue(binarySearchTree.Insert(input, data));
      }
  </code></pre>