C# ToArray

Array type

ToArray converts types to arrays. It forces the evaluation of query expressions and IEnumerable types. It is implemented in the System.Linq namespace. It is convenient in many C# programs. It reduces program length and increases program simplicity.

This C# tutorial shows how to use the ToArray extension method from System.Linq.

Example

LINQ (language integrated query)

This program shows a query expression using the query syntax in the C# language. The program takes an input integer array and then sorts it using the LINQ syntax, which internally drills down to a quick sort in many cases.

The query is not immediately executed, but the ToArray method forces its evaluation into an array variable with elements stored in contiguous memory on the managed heap. The program then displays these sorted array elements.

Program that uses ToArray extension method [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	//
	// An example array.
	//
	int[] array1 = { 5, 4, 1, 2, 3 };
	//
	// 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);
	}
    }
}

Output

1
2
3
4
5
Var keyword

Query expression used. The var implicitly typed syntax is a perfect fit for the query expression that declares an ascending sort of the array input element integers. After the query expression is declared and the supporting data structures are stored in memory, it is executed by the ToArray extension method invocation.

Var Examples

ToArray forces evaluation. The second int[] local variable is assigned to the memory allocated on the managed heap by the internal implementation of the ToArray method. The ToArray extension method is a generic method that internally allocates a Buffer array where the elements are copied. In this program, the elements are of type Int32.

ToArray methods

Note

There are other ToArray methods in the C# programming language. It is important to realize that the "ToArray" method identifier is used by various different implementations on different types. In particular, the List generic type has a ToArray method that is implemented on the List type.

List Examples

However, you can invoke the ToArray extension method on the List type, which does the same thing but may have some performance disadvantages. Additionally, the ArrayList class contains a ToArray method that is implemented on its custom type.

Convert List to Array

Summary

The C# programming language

We looked at an example of the ToArray extension method from the System.Linq extension namespace in the C# programming language. We noted how this method can be used to convert a query expression variable into an array type. We next visited the concept of lazy evaluation in query expressions and how the ToArray extension method is implemented. Finally we mentioned other ToArray methods.

LINQ Examples
.NET