Method ToArrayByIndex
| Edit this pageToArrayByIndex<T>(IEnumerable<T>, Func<T, int>)
Creates an array from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array.
Declaration
public static T?[] ToArrayByIndex<T>(this IEnumerable<T> source, Func<T, int> indexSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
Returns
| Type | Description |
|---|---|
| T[] | An array that contains the elements from |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(c => c[0] - 'a');
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [alp, bar, car, , , foo]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException | An index returned by |
ToArrayByIndex<T, TResult>(IEnumerable<T>, Func<T, int>, Func<T, TResult>)
Creates an array from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array. The elements are projected into the array via an additional function.
Declaration
public static TResult?[] ToArrayByIndex<T, TResult>(this IEnumerable<T> source, Func<T, int> indexSelector, Func<T, TResult> resultSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
| Func<T, TResult> | resultSelector | A function to project a source element into an element of the resulting array. |
Returns
| Type | Description |
|---|---|
| TResult[] | An array that contains the projected elements from |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
| TResult | The type of the element in the resulting array. |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(c => c[0] - 'a', c => $"{c[0] - 'a'}:{c}");
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0:alp, 1:bar, 2:car, , , 5:foo]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException | An index returned by |
ToArrayByIndex<T, TResult>(IEnumerable<T>, Func<T, int>, Func<T, int, TResult>)
Creates an array from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array. The elements are projected into the array via an additional function.
Declaration
public static TResult?[] ToArrayByIndex<T, TResult>(this IEnumerable<T> source, Func<T, int> indexSelector, Func<T, int, TResult> resultSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
| Func<T, int, TResult> | resultSelector | A function to project a source element into an element of the resulting array. |
Returns
| Type | Description |
|---|---|
| TResult[] | An array that contains the projected elements from |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
| TResult | The type of the element in the resulting array. |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(c => c[0] - 'a', (c, i) => $"{i}:{c}");
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0:alp, 1:bar, 2:car, , , 5:foo]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException | An index returned by |
ToArrayByIndex<T>(IEnumerable<T>, int, Func<T, int>)
Creates an array of user-specified length from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array.
Declaration
public static T?[] ToArrayByIndex<T>(this IEnumerable<T> source, int length, Func<T, int> indexSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| int | length | The (non-negative) length of the resulting array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
Returns
| Type | Description |
|---|---|
| T[] | An array of size |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(26, c => c[0] - 'a');
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [alp, bar, car, , , foo, , , , , , , , , , , , , , , , , , , , ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
ToArrayByIndex<T, TResult>(IEnumerable<T>, int, Func<T, int>, Func<T, TResult>)
Creates an array of user-specified length from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array. The elements are projected into the array via an additional function.
Declaration
public static TResult?[] ToArrayByIndex<T, TResult>(this IEnumerable<T> source, int length, Func<T, int> indexSelector, Func<T, TResult> resultSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| int | length | The (non-negative) length of the resulting array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
| Func<T, TResult> | resultSelector | A function to project a source element into an element of the resulting array. |
Returns
| Type | Description |
|---|---|
| TResult[] | An array of size |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
| TResult | The type of the element in the resulting array. |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(26, c => c[0] - 'a', (c, i) => $"{i}:{c}");
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0:alp, 1:bar, 2:car, , , 5:foo, , , , , , , , , , , , , , , , , , , , ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
ToArrayByIndex<T, TResult>(IEnumerable<T>, int, Func<T, int>, Func<T, int, TResult>)
Creates an array of user-specified length from an IEnumerable<T> where a function is used to determine the index at which an element will be placed in the array. The elements are projected into the array via an additional function.
Declaration
public static TResult?[] ToArrayByIndex<T, TResult>(this IEnumerable<T> source, int length, Func<T, int> indexSelector, Func<T, int, TResult> resultSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source sequence for the array. |
| int | length | The (non-negative) length of the resulting array. |
| Func<T, int> | indexSelector | A function that maps an element to its index. |
| Func<T, int, TResult> | resultSelector | A function to project a source element into an element of the resulting array. |
Returns
| Type | Description |
|---|---|
| TResult[] | An array of size |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the element in |
| TResult | The type of the element in the resulting array. |
Remarks
This method forces immediate query evaluation. It should not be used on infinite sequences. If more than one element maps to the same index then the latter element overwrites the former in the resulting array.
Examples
The following code example demonstrates how to transform a sequence into an array using indices using ToArrayByIndex.
var sequence = new[] { "foo", "bar", "alp", "car", };
// Transform a sequence by index
var result = sequence
.ToArrayByIndex(26, c => c[0] - 'a', c => $"{c[0] - 'a'}:{c}");
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0:alp, 1:bar, 2:car, , , 5:foo, , , , , , , , , , , , , , , , , , , , ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|