CountOccurrences - Kalkwst/Risotto GitHub Wiki

Definition

Counts the occurrences of the element in the source sequence.

In this page

Overloads

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.

CountOccurrences(T element)

Counts the occurrences of the element in the source sequence.

public static int CountOccurrences<T>(this IEnumerable<T> source, T element);

Type Parameters

T
The type of the elements of source.

Parameters

source IEnumerable <TSource>
The source sequence.

element
The element to count occurrences of.

Returns

Int32
The number of occurrences if any

Exceptions

ArgumentNullException
source is null

Example

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);
//=> 2

CountOccurrences(T element, IEqualityComparer comparer)

Counts 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);

Type Parameters

T
The type of the elements of source.

Parameters

source IEnumerable <TSource>
The source sequence.

element
The element to count occurrences of.

comparerIEqualityComparer
An equality comparer to compare values.

Returns

Int32
The number of occurrences if any

Exceptions

ArgumentNullException
source is null

-or-

comparer is null

Example

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
⚠️ **GitHub.com Fallback** ⚠️