.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Convert ArrayList to Array

Posted By:Pankaj Mishra       Posted Date: August 13, 2008    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  

Using this code you can convert arraylist to array of its native type. Its very easy and simple and using one line of code you can achieve that.
 

Using this code you can convert arraylist to array of its native type.. Its very easy and simple. Below is the way to convet ArrayList to Array

In C# code sample

ArrayList arrList= new ArrayList();
int []arrToConvert = new int[]{};

//Add some value to ArrayList of type integer
arrList.Add(1);
arrList.Add(2);
arrList.Add(3);
arrList.Add(4);

//Now convert ArrayList to array
arrToConvert = (int[])arrList.ToArray(typeof(Int32));

In the above example we have taken Integer value and stored in the array list so while conversion we have to convert of array type of integer

Vb.Net sample code

Dim arrList As New ArrayList()
Dim arrToConvert As Integer() = New Integer() {}

'Add some value to ArrayList
arrList.Add(1)
arrList.Add(2)
arrList.Add(3)
arrList.Add(4)

'Now convert ArrayList to array
arrToConvert = DirectCast(arrList.ToArray(GetType(Int32)), Integer())
Similarly you can convert any type of datatype to array like string, decimal, float double datatype or object 

Cheers
Pankaj

 

 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend