Home
Map
ToArray Extension MethodUse the ToArray extension method from System.Linq. Convert a query, or any IEnumerable, into an array.
C#
This page was last reviewed on Apr 22, 2023.
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. 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.
Generic
Step 1 The program takes an input integer array. This array has 3 int elements.
Step 2 We sort the array using the LINQ syntax, which internally drills down to a quick sort. We use var for the type of the query expression.
var
Step 3 ToArray() forces evaluation. The int[] variable is assigned to the memory allocated on the managed heap by ToArray.
int Array
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.
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.
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
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.
This page was last updated on Apr 22, 2023 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.