LinkedList<T>.First property is used to get the first node of the LinkedList<T>.
Syntax:
CSHARP
Output:
CSHARP
public System.Collections.Generic.LinkedListNode First { get; }
Return Value: The first LinkedListNode<T> of the LinkedList<T>.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to get the first
// node of the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a LinkedList of Strings
LinkedList<String> myList = new LinkedList<String>();
// Adding nodes in LinkedList
myList.AddLast("Geeks");
myList.AddLast("for");
myList.AddLast("Data Structures");
myList.AddLast("Noida");
// To get the first node of the LinkedList
if (myList.Count > 0)
Console.WriteLine(myList.First.Value);
else
Console.WriteLine("LinkedList is empty");
}
}
GeeksExample 2:
// C# code to get the first
// node of the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a LinkedList of Integers
LinkedList<int> myList = new LinkedList<int>();
// To get the first node of the LinkedList
if (myList.Count > 0)
Console.WriteLine(myList.First.Value);
else
Console.WriteLine("LinkedList is empty");
}
}
Output:
LinkedList is emptyNote:
- LinkedList accepts null as a valid Value for reference types and allows duplicate values.
- If the LinkedList is empty, the First and Last properties contain null.
- Retrieving the value of this property is an O(1) operation.