ToArray. This method converts types to arrays. It forces the evaluation of query expressions and IEnumerable types. It returns an array of the same type.
Implemented in System.Linq, ToArray reduces program length and increases program simplicity. But we must be careful not to call ToArray when it is not needed.
Example. The ToArray extension method is a generic method that internally allocates a Buffer array where the elements are copied. We often use it alongside query expressions.
Step 4 We display the array returned by ToArray() with a foreach loop, printing it to the console.
using System;
using System.Linq;
// Step 1: create an array.
int[] array1 = { 5, 1, 4 };
// Step 2: use query expression on array.
var query = from element in array1
orderby element
select element;
// Step 3: convert expression to array variable.
int[] array2 = query.ToArray();
// Step 4: display array.
foreach (int value in array2)
{
Console.WriteLine(value);
}1
4
5
ToArray, same statement. Suppose we have a query expression, and we want to have an array. We can call ToArray directly on the query expression, which forces its evaluation.
Tip For methods like ToArray, Average(), and Sum(), we often want to directly invoke the extension method on a query.
using System;
using System.Linq;
class Program
{
static void PrintArrayLength(int[] array)
{
Console.WriteLine("ARRAY LENGTH: {0}", array.Length);
}
static void Main()
{
int[] elements = { 10, 20, 30 };
// Use ToArray directly on query.
PrintArrayLength((from e in elements
where e >= 20
select e).ToArray());
}
}ARRAY LENGTH: 2
Not always needed. We can sometimes avoid using ToArray. Suppose want to Join together the strings in a string List. We can directly pass the List to string.Join.
Note We can call ToArray and then pass the array to Join(), but this will not give us any advantage.
And Using ToArray when we do not need to can cause a performance loss (one that is proportional to the element count).
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var items = new List<string>() { "test", "deploy", "script" };
// We can use string.Join without ToArray.
Console.WriteLine("NO TOARRAY: {0}", string.Join("+", items));
Console.WriteLine(" TOARRAY: {0}", string.Join("+", items.ToArray()));
}
}NO TOARRAY: test+deploy+script
TOARRAY: test+deploy+script
Discussion. There are other ToArray methods. It is important to realize that the "ToArray" method identifier is used by various different implementations on different types.
Note The List generic type has a ToArray method that is implemented on the List type.
Summary. ToArray converts an enumerable types into an array. It can act upon a query expression variable. It forces evaluation of a query expression.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2024 (edit).