List to Array

In this chapter you will learn:

  1. How to convert List to Array

Convert List to Array

using System;//from jav  a2  s  .  c  o  m
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        //Create a List of Strings
        //String-typed list
        List<string> words = new List<string>();  

        words.Add("A");
        words.Add("B");
        words.Add("C");
        words.Add("D");
        words.Add("E");
        words.Add("F");


        string[] wordsArray = words.ToArray();  // Creates a new typed array

        // Copy first two elements to the end of an existing array:
        string[] existing = new string[1000];
        words.CopyTo(0, existing, 998, 2);
   }
}

Next chapter...

What you will learn in the next chapter:

  1. SortedList and key/value pair