Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Copying the SortedList elements to an Array Object
Next article icon

C# | Copying the elements of ArrayList to a new array

Last Updated : 04 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows:
 

  1. ToArray()
  2. ToArray(Type)

ToArray()

This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.
Syntax:
 

public virtual object[] ToArray ();

Return Value: This method will return an Object array containing copies of the elements of the ArrayList.
Example:
 

CSharp




// C# program to illustrate ToArray() Method
using System;
using System.Collections;
 
class GFG {
     
    // Main Method
    public static void Main()
    {
         
        // Create and initializing ArrayList
        ArrayList mylist = new ArrayList(5);
         
        mylist.Add("G");
        mylist.Add("E");
        mylist.Add("E");
        mylist.Add("K");
        mylist.Add("S");
 
         
        // Copy the data of Arraylist into
        // the object Array Using ToArray()
        // method
        object[] str2 = mylist.ToArray();
         
        foreach(string i in str2)
        {
            Console.WriteLine(i);
        }
    }
}
 
 
Output: 
G E E K S

 

ToArray(Type)

This method is used to copy the elements of the ArrayList to a new array of the specified element type. The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.
Syntax: 
 

public virtual Array ToArray (Type t);

Here, t is the element Type of the destination array to create and copy elements to. 
Return Value : This method will return an array of the specified element type containing copies of the elements of the ArrayList.
Exception: 
 

  • If the value of t is null then this method will give ArgumentNullException.
  • If the type of the source ArrayList cannot be cast automatically to the specified type, then this method will give InvalidCastException.

Note: All of the objects in the ArrayList object will be cast to the Type specified in the type parameter.
Example:
 

CSharp




// C# program to illustrate ToArray(Type) Method
using System;
using System.Collections;
 
class GFG {
     
    // Main Method
    public static void Main()
    {
         
        // Create and initialize new array
        ArrayList mylist = new ArrayList(5);
        mylist.Add("G");
        mylist.Add("E");
        mylist.Add("E");
        mylist.Add("K");
        mylist.Add("S");
 
        // Copy the data of Arraylist into
        // the string Array Using
        // ToArray(Type) method
        string[] str2 = (string[])mylist.ToArray(typeof(string));
 
        // Display the data of str2 string
        foreach(string i in str2)
        {
            Console.WriteLine(i);
        }
    }
}
 
 
Output: 
G E E K S

 

Reference: 
 

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.toarray?view=netframework-4.7.2

 



Next Article
C# | Copying the SortedList elements to an Array Object
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Collections-ArrayList
  • CSharp-Collections-Namespace
  • CSharp-method

Similar Reads

  • C# | Copying BitArray elements to an Array
    The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent
    3 min read
  • C# | How to copy the entire ArrayList to a one-dimensional Array
    ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList
    3 min read
  • Copying the Queue elements to 1-D Array in C#
    Queue<T>.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, whe
    4 min read
  • C# | How to convert an ArrayList to Array
    In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen
    4 min read
  • C# | Copying the SortedList elements to an Array Object
    SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat
    2 min read
  • C# | Get the number of elements actually contained in the ArrayList
    ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in
    3 min read
  • C# | Adding elements to the end of the ArrayList
    AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Or in other words, this method is used to add the multiple elements from other collection into an ArrayList. Here elements are defined as the primitive or non-primitive type. Syntax: public virtua
    3 min read
  • C# | Copying the Collection<T> elements to an array
    Collection<T>.CopyTo(T[], Int32) method is used to copy the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : The one-dimensional Array that is the destin
    3 min read
  • C# | Copying the Hashtable elements to an Array Instance
    Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry
    3 min read
  • C# | Sort the elements in the ArrayList
    ArrayList.Sort Method is used to sorts the elements in the ArrayList. There are total 3 methods in the overload list of this method as follows: Sort()Sort(IComparer)Sort(Int32, Int32, IComparer)Sort() This method is used to Sort the elements in the entire ArrayList. It uses the QuickSort algorithm t
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences