C# | Get the first node of the LinkedList<T>
LinkedList<T>.First property is used to get the first node of the LinkedList<T>.
Syntax:
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"); } } |
chevron_right
filter_none
Output:
Geeks
Example 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"); } } |
chevron_right
filter_none
Output:
LinkedList is empty
Note:
- 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.
Reference:
Recommended Posts:
- C# | Get the last node of the LinkedList<T>
- C# | Adding new node or value at the end of LinkedList<T>
- C# | Removing the node at the end of LinkedList<T>
- C# | Removing the specified node from the LinkedList<T>
- C# | Find the first node in LinkedList<T> containing the specified value
- C# | Find the last node in LinkedList<T> containing the specified value
- C# | Adding new node or value at the start of LinkedList<T>
- C# | Removing the node at the start of the LinkedList<T>
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



