LinkedList.AddBefore Method (Generic LinkedListNode, Generic LinkedListNode)

Adds the specified new node before the specified existing node in the LinkedList.

Namespace: System.Collections.Generic
Assembly: System (in system.dll)

void AddBefore (
	LinkedListNode<T>^ node, 
	LinkedListNode<T>^ newNode
public void AddBefore (
	LinkedListNode<T> node, 
	LinkedListNode<T> newNode
public function AddBefore (
	node : LinkedListNode<T>, 
	newNode : LinkedListNode<T>
Not applicable.



The LinkedListNode before which to insert newNode.


The new LinkedListNode to add to the LinkedList.

Exception typeCondition


node is a null reference (Nothing in Visual Basic).


newNode is a null reference (Nothing in Visual Basic).


node is not in the current LinkedList.


newNode belongs to another LinkedList.

LinkedList accepts a null reference (Nothing in Visual Basic) as a valid Value for reference types and allows duplicate values.

This method is an O(1) operation.

The following code example and output demonstrate the AddBefore method. The code example begins by marking the existing node containing "fox". The code then finds the node containing "dog" and attempts to use the AddBefore(Generic LinkedListNode,Generic LinkedListNode) method overload to add "fox" before "dog", without first removing "fox" from the list. This causes an InvalidOperationException, which is caught. The code example then removes "fox" and uses the AddBefore(Generic LinkedListNode,Generic LinkedListNode) method overload to add it before "dog". Finally, the code example removes "dog" and inserts it at the location formerly occupied by "fox".

This code and output are part of a larger example provided for the LinkedList class.

// Keep a reference to the current node, "fox", and to the
// previous node in the list. Use the Find method to locate
// the node containing the value "dog". Show the position.
mark1 = current;
LinkedListNode<String^>^ mark2 = current->Previous;
current = sentence->Find("dog");

// The AddBefore method throws an InvalidOperationException
// if you try to add a node that already belongs to a list.
    sentence->AddBefore(current, mark1);
catch(InvalidOperationException^ ex)
    Console::WriteLine("Exception message: {0}", ex->Message);

// Remove the node referred to by mark1, and add it before 
// the node referred to by current. Show the sentence, 
// highlighting the position of the node referred to by
// current.
sentence->AddBefore(current, mark1);

// Remove the node referred to by current. If you try to show
// its position now, the DisplayNode method prints a message.
// Add the node after the node referred to by mark2, and 
// display the sentence, highlighting current.
sentence->AddAfter(mark2, current);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions