LINQ ToArray() Method

In LINQ, ToArray() operator is used to convert the input elements in collection to an Array. 

Syntax of LINQ ToArray() Operator

Following is the syntax of using LINQ ToArray() operator to convert the collection to an array.

 LINQ ToArray() Syntax in C#

string[] countryarray = countries.ToArray();

LINQ ToArray() Syntax in VB.NET

Dim countryarray As String() = countries.ToArray()

If you observe the above syntax we are converting “countries” collection to an array.

Example of LINQ ToArray() Operator in Method Syntax

Following is the example of using LINQ ToArray() operator in method syntax to convert input collection items to the new array.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };
      string[] countryarray = countries.ToArray();
      foreach (string s in countryarray)
      {
        Console.WriteLine(s);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"US", "UK", "India", "Russia", "China", "Australia", "Argentina"}
Dim countryarray As String() = countries.ToArray()
For Each s As String In countryarray
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

If you observe the above example, we have a List of type string “countries”. Using the ToArray () method, we converted the “countries” List to an array. We have iterated the array in a foreach loop and displayed it on the screen to access these elements.

Output of LINQ ToArray() in Method Syntax Example

Following is the result of the LINQ ToArray() operator in the method syntax example.

 

US
UK
India
Russia
China
Australia
Argentina

LINQ ToArray() Operator in Query Syntax Example

Following is the example of using the LINQ ToArray() operator in query syntax.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };
      string[] countryarray = (from x in countries select x).ToArray();
      foreach (string s in countryarray)
      {
        Console.WriteLine(s);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"US", "UK", "India", "Russia", "China", "Australia", "Argentina"}
Dim countryarray As String() = (From x In countries).ToArray()
For Each s As String In countryarray
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

Output of LINQ ToArray() in Query Syntax Example

Following is the result of LINQ ToArray() in query syntax example.

 

US
UK
India
Russia
China
Australia
Argentina

This is how we can use LINQ ToArray to convert given collection items to an array list using method syntax and query syntax based on our requirements.