2

I have an ArrayList, need to add the items of ArrayList into a List of string. I tried the following:

listString.AddRange(new List<string>((string[])this.arrayList.ToArray()))

But this gives an exception of

Unable to cast object of type 'System.Object[]' to type 'System.String[]'

Please note :; I can't use .Cast as I am working in framework 4.0.

0

7 Answers 7

4

Use ToArray(typeof(string)) to create an Array and then cast it to a more specific array type string[] before adding it to the variable of type List<string>

listString.AddRange((string[])this.arrayList.ToArray(typeof(string)));
Sign up to request clarification or add additional context in comments.

1 Comment

That actually worked. That was a great help. Thanks a lot.
3

You can try the below code as follows:

ArrayList al = new ArrayList();
 List<string> lst = new List<string>();
 foreach (string l in al)
 {
    lst.Add(l);
 }

Comments

2

LINQ to objects has a Cast method, which returns an IEnumerable of T where all the types have been cast (assuming the cast is valid) , so i'd suggest:

this.ArrayList.Cast<string>().ToList();

or, if your listString is already existing:

listString.AddRange(this.ArrayList.Cast<string>());

1 Comment

Yes, i did. But check MSDN on Cast(). It is supported since 3.5
1

You have to convert each elements of the array list into a string to add them in a List of strings. Better option for doing this is like the following:

 listString.AddRange(arrayList.ToArray().Select(x => x.ToString()).ToList());

2 Comments

I am not able to use Select/ Cast/ ToList(). There isn't any definition for these all in ArrayList. However, the answer given by Svein Fidjestøl worked.
To make it work you have to add using System.Linq; to the using section
1

It seems to me, the safest way:

listString.AddRange(arrayList.ToArray().Select(e => e.ToString()));

Comments

0
ArrayList arrayList = new ArrayList {"A","B","C" };
List<string> listString = new List<string>();  
foreach (string item in arrayList)
{
    listString.Add(item);
}
var data = listString;

Comments

-4

Try to understand below code : this code solve problem.

ArrayList myAL = new ArrayList(); 
myAL.Add( "The" );
myAL.Add( "quick" );    
myAL.Add( "brown" ); 
myAL.Add( "fox" ); 

// Creates and initializes a new Queue.
Queue myQueue = new Queue();
myQueue.Enqueue( "jumped" ); 
myQueue.Enqueue( "over" ); 

// Displays the ArrayList and the Queue.  
Console.WriteLine( "The ArrayList initially contains the following:" );
PrintValues( myAL, '\t' );
Console.WriteLine( "The Queue initially contains the following:" ); 
PrintValues( myQueue, '\t' ); 

// Copies the Queue elements to the end of the ArrayList. 
myAL.AddRange( myQueue );

1 Comment

Indeed this is hard to understand. Not sure why one would spend time to "try to understand" it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.