CSharp - LINQ LongCount

Introduction

The LongCount operator returns the number of elements in the input sequence as a long.

Prototypes

There are two prototypes we cover. The First LongCount Prototype

public static long LongCount<T>(
  this IEnumerable<T> source);

The first prototype of the LongCount operator returns the total number of elements in the source input sequence by enumerating the entire input sequence and counting the number of elements.

The second prototype of the LongCount operator enumerates the source input sequence and counts every element by the predicate method.

public static long LongCount<T>(
   this IEnumerable<T> source,
   Func<T, bool> predicate);

Exceptions

ArgumentNullException is thrown if any argument is null.

The following code generated two sequences using the Range operator and concatenated them together using the Concat operator.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program//  w  w w  . j av  a  2s. c  o  m
{
    static void Main(string[] args)
    {
          long count = Enumerable.Range(0, int.MaxValue).
            Concat(Enumerable.Range(0, int.MaxValue)).LongCount();
    
          Console.WriteLine(count);
    }
}

Result

Related Topics