C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Finally: We prove that the array now contains all the elements of the originating List.
C# program that uses CopyTo on List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a list with three elements.
var list = new List<int>() { 5, 6, 7 };
// Create an array with length of three.
int[] array = new int[list.Count];
// Copy the list to the array.
list.CopyTo(array);
// Display.
Console.WriteLine(array[0]);
Console.WriteLine(array[1]);
Console.WriteLine(array[2]);
}
}
Output
5
6
7
Note: The .NET Framework developers probably know more about the best way to do things than do most of us.
Also: The type of the elements in the array must be equivalent to the type of the List elements as well.