c# program convert array list to array example

C# Program to convert ArrayList to Array Example

  • This csharp program is about how to convert array list to array
  • In this c# Program first we have created ArrayList Object and named it as arrayList
  • In order to use ArrayList in our program we have to import System.Collections namespace.
  • After creating arrayList we have added items to the arrayList Object using arrayList.Add() method.
  • To store arrayList items to an array we have created an string array object.
  • Finally using  arrayList.ToArray(typeof(string)) as string[]; we have converted Arraylist to an array.
  • Following is the program to convert ArrayList to Array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ArrayListToArrayProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrayList = new ArrayList();
            arrayList.Add("AAAAAAAA");
            arrayList.Add("BBBBBBBB");
            arrayList.Add("CCCCCCCC");
            arrayList.Add("DDDDDDDD");

            string[] array = arrayList.ToArray(typeof(string)) as string[];

            Console.WriteLine("Contents of Array are:");
            foreach (var item in array)
            {
                Console.WriteLine(item);
            }
        }
    }
}



Output of Arraylist to array program.

Contents of Array are:
AAAAAAAA
BBBBBBBB
CCCCCCCC
DDDDDDDD
Press any key to continue 


0 comments:

Post a Comment