103

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lomaxx
  • 113,627
  • 57
  • 144
  • 179

8 Answers8

157

.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(); });
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165
125

Updated for 2010

List<int> l1 = new List<int>(new int[] { 1,2,3 } );
List<string> l2 = l1.ConvertAll<string>(x => x.ToString());
Luke
  • 1,916
  • 1
  • 15
  • 15
  • 6
    Thanks! This saved me a ton of time – Steve French Oct 25 '10 at 19:52
  • 2
    No, lambdas were introduced in C# 3.0 so this will not work in 2.0. – Luke Feb 28 '12 at 17:08
  • I found that it works with VS2008 and .NET 2.0, as long as you have at least .NET 3.0 installed. see http://stackoverflow.com/questions/3341846/use-certain-lambda-expressions-when-targeting-net-2-0 – igelineau Oct 15 '14 at 14:26
  • 1
    People should differentiate between .NET version and C# version. Since the arrow `x => x.ToString()` is compiled to the same kind of IL as is `delegate(int x) { return x.ToString(); }` in this case the important thing is to have a C# compiler (C# version) that knows what `=>` is. The framework and runtime (.NET version) needs no special features for this, so .NET 2.0 is fine here. – Jeppe Stig Nielsen Feb 13 '15 at 20:38
  • 1
    Turns out you can ditch `` and it still works. Making it just `l1.ConvertAll(x => x.ToString());` – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Oct 06 '16 at 15:07
8

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.


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

halfer
  • 19,824
  • 17
  • 99
  • 186
Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66
7

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();
lutecki
  • 339
  • 4
  • 5
5

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.

ljs
  • 37,275
  • 36
  • 106
  • 124
  • 3
    The existence of a cast between the generic types has nothing to do with the ability to cast between the projection. IE, even if `int` were implicitly or explicitly convertible to `string`, that doesn't mean that `List` has an implicit or explicit conversion to `List`. – Adam Robinson Jan 22 '10 at 20:29
3

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

replace the parameters result and listOfInt to your parameters

Jayant Rajwani
  • 727
  • 9
  • 16
2

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();
Zuabros
  • 252
  • 1
  • 5
1

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.

Curt Hagenlocher
  • 20,680
  • 8
  • 60
  • 50
  • [@Curt](http://stackoverflow.com/questions/44942/cast-listint-to-liststring#44950): > It is theoretically possible to treat > a List as a List -- > this gets you into the exciting worlds > of covariance and contravariance, and > is not currently supported by C# or > VB.NET C# and .NET does actually support Covariance. Just not with generics. – Christian Hagelid Sep 04 '08 at 23:57