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

C#GenericsCasting

C# Problem Overview


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).

C# Solutions


Solution 1 - C#

.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(); });

Solution 2 - C#

Updated for 2010

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

Solution 3 - C#

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.Add(1);
list.Add(2);
list.Add(3);
list.Convert(delegate (int i) { return i.ToString(); });

Something along those lines.


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

Solution 4 - C#

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();

Solution 5 - C#

You wouldn't be able to directly cast it as no explicit or implicit cast exists from int to string, it would have to be a method involving .ToString() such as:-

foreach (int i in intList) stringList.Add(i.ToString());

Edit - or as others have pointed out rather brilliantly, use intList.ConvertAll(delegate(int i) { return i.ToString(); });, however clearly you still have to use .ToString() and it's a conversion rather than a cast.

Solution 6 - C#

result = listOfInt.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToList()

replace the parameters result and listOfInt to your parameters

Solution 7 - C#

Converting from int List to string List can be done in two adittional ways besides the usual ToString(). Choose the one that pleases you more.

var stringlist = intlist.Select(x=>""+x).ToList();

Or also:

var stringlist = intlist.Select(x=>$"{x}").ToList();

And finally the traditional:

var stringlist = intlist.Select(x=>x.ToString()).ToList();

Solution 8 - C#

You have to build a new list. The underlying bit representations of List<int> and List<string> are completely incompatible -- on a 64-bit platform, for instance, the individual members aren't even the same size.

It is theoretically possible to treat a List<string> as a List<object> -- this gets you into the exciting worlds of covariance and contravariance, and is not currently supported by C# or VB.NET.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlomaxxView Question on Stackoverflow
Solution 1 - C#Glenn SlavenView Answer on Stackoverflow
Solution 2 - C#LukeView Answer on Stackoverflow
Solution 3 - C#Erik van BrakelView Answer on Stackoverflow
Solution 4 - C#luteckiView Answer on Stackoverflow
Solution 5 - C#ljsView Answer on Stackoverflow
Solution 6 - C#Jayant RajwaniView Answer on Stackoverflow
Solution 7 - C#ZuabrosView Answer on Stackoverflow
Solution 8 - C#Curt HagenlocherView Answer on Stackoverflow