Home
Search
ToArray Extension MethodUse the ToArray extension method from System.Linq. Convert a query, or any IEnumerable, into an array.
C#
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.
IEnumerable
ToList
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.
An example. We see a query expression. The program takes an input integer array. It then sorts it using the LINQ syntax, which internally drills down to a quick sort.
Var We use var for the type of the query expression that declares a sort of the array input element integers.
Var
Note The query is not immediately executed. But the ToArray method forces its evaluation (with iteration) into an array variable.
Next ToArray() forces evaluation. The second int[] variable is assigned to the memory allocated on the managed heap by ToArray.
Int Array
Tip The ToArray extension method is a generic method that internally allocates a Buffer array where the elements are copied.
Generic Class, Method
using System; using System.Linq; class Program { static void Main() { int[] array1 = { 5, 1, 4 }; // Use query expression on array. var query = from element in array1 orderby element select element; // Convert expression to array variable. int[] array2 = query.ToArray(); // 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.
Average
Sum
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.
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
A 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.
List
However You can invoke the ToArray extension method on the List type, which does the same thing but has some performance disadvantages.
Convert List, Array
Also The ArrayList class contains a ToArray method that is implemented on its custom type.
ArrayList
A 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.
No updates found for this page.
Home
Changes
© 2007-2022 Sam Allen.