TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Count Extension Method

This C# tutorial shows different ways to use the Count extension from System.Linq.

Count. The Count extension method enumerates elements.

In many cases, the Count() extension is not useful, but in some program contexts it is appropriate. Most collections have a Length or Count property that is more efficient.

Array Length

Example. The Count() extension method is found in System.Linq. It always acts upon an IEnumerable type. This means it works on Lists and arrays, even when those types have other counting properties (Count and Length).

Note: When Count() is called on Lists or arrays, you lose some performance over calling the direct properties available.

C# program that uses Count extension

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array = { 1, 2, 3 };

	// Don't use Count() like this! Use Length.
	Console.WriteLine(array.Count());

	List<int> list = new List<int>() { 1, 2, 3 };

	// Don't use Count() like this! Use Count property.
	Console.WriteLine(list.Count());

	var result = from element in array
		     orderby element descending
		     select element;

	// Good.
	Console.WriteLine(result.Count());
    }
}

Output

3
3
3

When to use Count(). This extension method does have some good uses. If you use a LINQ query expression, or for some other reason have an IEnumerable instance, it is the best way to determine how many elements are present.

IEnumerable Examples: LINQ, Lists and Arrays

Example 2. The Count method can be used with an argument of type Func. The Func can be specified with a lambda expression. In this example we count only int elements greater than 2. The values 3, 4 and 5—three elements—are greater than 2.

FuncLambda Expression

C# program that uses Count with argument

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array = { 1, 2, 3, 4, 5 };
	// ... Count only elements greater than 2.
	int greaterThanTwo = array.Count(element => element > 2);
	Console.WriteLine(greaterThanTwo);
    }
}

Output

3

Performance. Also, there are some Count extension method benchmarks on this site. The Length and Count properties are compared to the Count extension method. Using a property on a collection is much faster than Count.

Count Array ElementsProperty

Summary. The Count extension method provides a way to get the number of elements in an IEnumerable collection by enumerating those elements. This is an inefficient way to get the element count of collections that store a cached field.

However: For IEnumerable instances that may not even know their lengths yet, the Count method can determine it.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf