Export (0) Print
Expand All

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)

public:
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.

Parameters

node

The LinkedListNode before which to insert newNode.

newNode

The new LinkedListNode to add to the LinkedList.

Exception typeCondition

ArgumentNullException

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

-or-

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

InvalidOperationException

node is not in the current LinkedList.

-or-

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");
DisplayNode(current);

// The AddBefore method throws an InvalidOperationException
// if you try to add a node that already belongs to a list.
try
{
    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->Remove(mark1);
sentence->AddBefore(current, mark1);
DisplayNode(current);

// 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->Remove(current);
DisplayNode(current);
sentence->AddAfter(mark2, current);
DisplayNode(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

ADD
Show:
© 2014 Microsoft