You can count the total number of elements or some specific elements in the array using an extension method Count() method.
The Count()
method is an extension method of IEnumerable included in System.Linq.Enumerable
class.
It can be used with any collection or a custom class that implements IEnumerable
interface. All the built-in collections in C#, such as array, ArrayList, List, Dictionary, SortedList, etc. implements IEnumerable, and so the Count()
method can be used with these collection types.
Count() Overloads
Count<TSource>() | Returns total number of elements in an array. |
Count<TSource>(Func<TSource, Boolean>) | Returns the total number of elements in an array that matches the specified condition using Func delegate. |
The following example displays the total number of elements in the array.
string[] empty = new string[5];
var totalElements = empty.Count(); //5
string[] animals = { "Cat", "Alligator", "fox", "donkey", "Cat", "alligator" };
totalElements = animals.Count(); //6
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2, 5, 6, 2, 2 };
totalElements = nums.Count(); //12
In the above example, the empty.Count() returns 5, even if there are no elements in the array. This is because an array already has five null elements. For others, it will return the total number of elements.
Count Specific Elements in an Array
The following example shows how to count the specific elements based on some condition.
string[] animals = { "Cat", "Alligator", "fox", "donkey", "Cat", "alligator" };
var totalCats = animals.Count(s => s == "Cat");
var animalsStartsWithA = animals1.Count(s => s.StartsWith("a", StringComparison.CurrentCultureIgnoreCase));
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2, 5, 6, 2, 2 };
var totalEvenNums = nums.Count(n => n%2==0);
You can also use Regex with the Count()
method, as shown below.
string[] animals = { "Cat", "Alligator", "fox", "donkey", "Cat", "alligator" };
var animalsWithCapitalLetter = animals.Count(s =>
{
return Regex.IsMatch(s, "^[A-Z]");
});
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- Searching in C# array
- Compare strings in C#
- How to combine two arrays without duplicate values in C#?
- Difference between String and string in C#.
- How to get a comma separated string from an array in C#?
- How to remove duplicate values from an array in C#?
- How to sort an array in C#?
- How to sort object array by specific property in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?
- Difference between Array and ArrayList