Method StartsWith
| Edit this page View SourceStartsWith<T>(IEnumerable<T>, IEnumerable<T>)
Determines whether the beginning of the first sequence is equivalent to the second sequence, using the default equality comparer.
Declaration
public static bool StartsWith<T>(this IEnumerable<T> first, IEnumerable<T> second)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The sequence to check. |
IEnumerable<T> | second | The sequence to compare to. |
Returns
Type | Description |
---|---|
bool | true if |
Type Parameters
Name | Description |
---|---|
T | Type of elements. |
Remarks
This is the IEnumerable<T> equivalent of StartsWith(string).
This method executes immediately.
Examples
The following code example demonstrates how to check that one sequence starts with the same elements as another sequence using StartsWith
.
var sequence = new[]
{
"BAR",
"foo",
"Baz",
"Qux",
"BAZ",
"FOO",
"bAr",
"baz",
"fOo",
"BaZ",
};
// check that sequence starts with a known sequence of values
var result = sequence
.StartsWith(new[] { "BAR", "foo", "Baz", });
Console.WriteLine($"StartsWith ['BAR', 'foo', 'Baz']: {result}");
result = sequence
.StartsWith(new[] { "bar", "foo", "Baz", });
Console.WriteLine($"StartsWith ['bar', 'foo', 'Baz']: {result}");
result = sequence
.StartsWith(new[] { "foo", "Baz", });
Console.WriteLine($"StartsWith ['foo', 'Baz']: {result}");
// This code produces the following output:
// StartsWith ['BAR', 'foo', 'Baz']: True
// StartsWith ['bar', 'foo', 'Baz']: False
// StartsWith ['foo', 'Baz']: False
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
StartsWith<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>?)
Determines whether the beginning of the first sequence is equivalent to the second sequence, using the specified element equality comparer.
Declaration
public static bool StartsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The sequence to check. |
IEnumerable<T> | second | The sequence to compare to. |
IEqualityComparer<T> | comparer | Equality comparer to use. |
Returns
Type | Description |
---|---|
bool | true if |
Type Parameters
Name | Description |
---|---|
T | Type of elements. |
Remarks
This is the IEnumerable<T> equivalent of StartsWith(string).
This method executes immediately.
Examples
The following code example demonstrates how to check that one sequence starts with the same elements as another sequence using StartsWith
.
var sequence = new[]
{
"BAR",
"foo",
"Baz",
"Qux",
"BAZ",
"FOO",
"bAr",
"baz",
"fOo",
"BaZ",
};
// check that sequence starts with a known sequence of values
var result = sequence
.StartsWith(new[] { "BAR", "foo", "Baz", }, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"StartsWith ['BAR', 'foo', 'Baz']: {result}");
result = sequence
.StartsWith(new[] { "bar", "foo", "Baz", }, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"StartsWith ['bar', 'foo', 'Baz']: {result}");
result = sequence
.StartsWith(new[] { "foo", "Baz", }, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"StartsWith ['foo', 'Baz']: {result}");
// This code produces the following output:
// StartsWith ['BAR', 'foo', 'Baz']: True
// StartsWith ['bar', 'foo', 'Baz']: True
// StartsWith ['foo', 'Baz']: False
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|