CountOccurrences - Kalkwst/Risotto GitHub Wiki
Counts the occurrences of the element in the source sequence.
In this page
- Definition
- Overloads
- CountOccurrences(T element)
- CountOccurrences(T element, IEqualityComparer comparer)
| Overload | Description |
|---|---|
| CountOccurrences(T element) | Counts the occurrences of the element in the source sequence. |
| CountOccurrences(T element, IEqualityComparer comparer) | Counts the occurrences of the element in the source sequence, using the provided comparer. |
Counts the occurrences of the element in the source sequence.
public static int CountOccurrences<T>(this IEnumerable<T> source, T element);T
The type of the elements of source.
source IEnumerable <TSource>
The source sequence.
element
The element to count occurrences of.
Int32
The number of occurrences if any
ArgumentNullException
source is null
The following code examples demonstrate how to use CountOccurrences(T element) to count all of the occurrences of an element in a sequence.
int[] sequence = new int[] { 1, 1, 2, 1, 1, 2, 1 };
sequence.CountOccurrences(2);
//=> 2Counts the occurrences of the element in the source sequence, using the provided comparer.
public static int CountOccurrences<T>(this IEnumerable<T> source, T element, IEqualityComparer<T> comparer);T
The type of the elements of source.
source IEnumerable <TSource>
The source sequence.
element
The element to count occurrences of.
comparerIEqualityComparer
An equality comparer to compare values.
Int32
The number of occurrences if any
ArgumentNullException
source is null
-or-
comparer is null
The following code example demonstrates how to use CountOccurrences(T element, IEqualityComparer comparer) to count all of the occurrences of an element in a sequence, using a custom comparer.
string[] array = new string[] { "a", "b", "B", "B", "c"};
array.CountOccurrences("b", StringComparer.OrdinalIgnoreCase);
//=> 3