Conversion of ArrayList to Array in C#

CsharpProgrammingServer Side Programming

To convert an ArrayList to Array, use the ToArray() method in C#.

Firstly, set an ArrayList −

ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");

Now, to convert, use the ToArray() method −

arrList.ToArray(typeof(string)) as string[];

Let us see the complete code −

Example

 Live Demo

using System;
using System.Collections;

public class Program {
   public static void Main() {
      ArrayList arrList = new ArrayList();
      arrList.Add("one");
      arrList.Add("two");
      arrList.Add("three");

      string[] arr = arrList.ToArray(typeof(string)) as string[];

      foreach (string res in arr) {
         Console.WriteLine(res);
      }
   }
}

Output

one
two
three
raja
Published on 31-Aug-2018 13:02:10
Advertisements