
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# ArrayList - ToArray() Method
The C# ArrayList ToArray() method is used to copy the elements of an 'ArrayList' into a new array object. It can also create a new array of a specified element type. This method allows us to work with the data in array format, which may be more suitable for certain contexts.
Syntax
Following are the syntax of the C# ArrayList ToArray() method −
public virtual Array ToArray(); public virtual Array ToArray (Type type);
Parameters
This method accepts a single parameter −
- type: It specifies the desired type of the elements in the returned array.
Return value
This method returns an array of the specified element types containing a copy of the elements of the ArrayList.
Example 1: ToArray the Entire ArrayList
Following is the basic example of the ToArray() method to convert an ArrayList into a Array object −
using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList() { 10, "Hello", 3.4, "tutorialspoint"}; object[] array = list.ToArray(); Console.WriteLine("Array Elements:"); foreach (var item in array) { Console.WriteLine(item); } } }
Output
Following is the output −
Array Elements: 10 Hello 3.4 tutorialspoint
Example 2: Convert an ArrayList into Integer Array
The below example creates an ArrayList with integer elements. We then use the ToArray() method to convert the ArrayList into an integer array −
using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList() { 1, 2, 3 }; // Convert to int array int[] intArray = (int[])list.ToArray(typeof(int)); Console.WriteLine("Integer Array Elements:"); foreach (var item in intArray) { Console.WriteLine(item); } } }
Output
Following is the output −
Integer Array Elements: 1 2 3
Example 3: Convert an ArrayList Into a String Array
Here, in this example, we use the ToArray() method to convert the ArrayList into a string array object −
using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList() { "Aman", "Rahhul", "Akash", "Vivek"}; // Convert to int array string[] strArray = (string[])list.ToArray(typeof(string)); Console.WriteLine("String Array Elements:"); foreach (var item in strArray) { Console.WriteLine(item); } } }
Output
Following is the output −
String Array Elements: Aman Rahhul Akash Vivek
Example 4: If ArrayList Contains Different Types of Elements
Here, in the following example, this method throws an 'InvalidCastException'. If the source ArrayList contains different types of elements, it can not be cast automatically to the specified type −
using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList() { "Aman", 'A', "Rahhul", 'R', "Akash", "Vivek", 'v'}; // Convert to int array string[] strArray = (string[])list.ToArray(typeof(string)); Console.WriteLine("String Array Elements:"); foreach (var item in strArray) { Console.WriteLine(item); } } }
Output
Following is the output −
Unhandled Exception: System.InvalidCastException