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:
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList<String> myList = new LinkedList<String>();
myList.AddLast( "Geeks" );
myList.AddLast( "for" );
myList.AddLast( "Data Structures" );
myList.AddLast( "Noida" );
if (myList.Count > 0)
Console.WriteLine(myList.Count);
else
Console.WriteLine( "LinkedList is empty" );
}
}
|
Output:
4
Example 2 :
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList< int > myList = new LinkedList< int >();
if (myList.Count > 0)
Console.WriteLine(myList.Count);
else
Console.WriteLine( "LinkedList is empty" );
}
}
|
Output :
LinkedList is empty
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article