Cast List<int> to List<string> in .NET 2.0

By : lomaxx

Can you cast a List<int> to List<string> somehow?

I know I could loop through and .ToString() the thing, but a cast would be awesome.

I'm in C# 2.0 (so no LINQ).

By : lomaxx


.NET 2.0 has the ConvertAll method where you can pass in a converter function:

List<int> l1 = new List<int>(new int[] { 1,2,3 } );
List<string> l2 = l1.ConvertAll<string>(delegate(int i) { return i.ToString(); });

Is C# 2.0 able to do List<T>.Convert? If so, I think your best guess would be to use that with a delegate:

List<int> list = new List<int>();
list.Convert(delegate (int i) { return i.ToString(); });

Something along those lines.

Upvote Glenn's answer, which is probably the correct code ;-)

You can use:

List<int> items = new List<int>(new int[] { 1,2,3 } );
List<string> s = (from i in items select i.ToString()).ToList();
By : lutecki

This video can help you solving your question :)
By: admin