C# | Get the number of nodes contained in LinkedList<T>

LinkedList<T>.Count property is used to get the number of nodes actually contained in the LinkedList<T>.

Syntax:

public int Count { get; }

Return Value: The number of nodes actually contained in the LinkedList.

Note: Retrieving the value of this property is an O(1) operation.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the number
// of nodes actually contained
// in 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 number of nodes actually
        // contained in the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.Count);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

chevron_right


Output:

4

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the number
// of nodes actually contained
// in 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 number of nodes actually
        // contained in the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.Count);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

chevron_right


Output :

LinkedList is empty

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.