Home

Search

C# ToArray Extension MethodUse the ToArray extension method from System.Linq. Convert a query, or any IEnumerable, into an array.
dot net perls

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

C# program that uses ToArray extension 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

C# program that uses ToArray directly on 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.

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).

C# program that avoids ToArray
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.

Home
© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.