Method CountBetween
| Edit this pageCountBetween<T>(IEnumerable<T>, int, int)
Determines whether or not the number of elements in the sequence is between an inclusive range of minimum and maximum integers.
Declaration
public static bool CountBetween<T>(this IEnumerable<T> source, int min, int max)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence |
| int | min | The minimum number of items a sequence must have for this function to return true. |
| int | max | The maximum number of items a sequence must have for this function to return true. |
Returns
| Type | Description |
|---|---|
| bool | true if the number of elements in the sequence is between (inclusive) the min and max given integers, false otherwise. |
Type Parameters
| Name | Description |
|---|---|
| T | Element type of sequence |
Remarks
This method executes immediately.
Examples
The following code example demonstrates how to check that a sequence length is between two numbers using CountBetween
var sequence = Enumerable.Range(1, 5);
foreach (var x in Enumerable.Range(3, 5))
{
// Check that a sequence has a length between two numbers
var result = sequence.CountBetween(x, x + 1);
Console.WriteLine($"CountBetween {x}-{x + 1}: {result}");
}
// This code produces the following output:
// CountBetween 3-4: False
// CountBetween 4-5: True
// CountBetween 5-6: True
// CountBetween 6-7: False
// CountBetween 7-8: False
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|