long
.
A long representing the number of elements in the sequence.
This method overload counts the elements in a sequence. For sequences with a large number of elements, it is more efficient to use this overload than the Count() overload, which returns an int.
If source contains more than int.MaxValue elements, this method returns the correct count as a long.
This method uses deferred execution.
using System;
using System.Collections.Generic;
using System.Linq;
public class LongCountExample
{
public static void Main(string[] args)
{
// Create a large list of integers.
// In a real scenario, this might be generated or loaded from a file.
var largeList = Enumerable.Range(1, 2_500_000_000).ToList(); // Intentionally large
// Use LongCount to get the total number of elements.
long count = largeList.LongCount();
Console.WriteLine($"The number of elements in the list is: {count}");
// Example with an empty list
var emptyList = new List();
long emptyCount = emptyList.LongCount();
Console.WriteLine($"The number of elements in the empty list is: {emptyCount}");
}
}