<p>Can you cast a <code>List&lt;int&gt;</code> to <code>List&lt;string&gt;</code> somehow?</p>

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

<p>I'm in C# 2.0 (so no <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="noreferrer">LINQ</a>).</p>

[{'answer_id': 44949, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 3, 'selected': False, 'text': "<p>Is C# 2.0 able to do <code>List&lt;T&gt;.Convert</code>? If so, I think your best guess would be to use that with a delegate:</p>\n<pre><code>List&lt;int&gt; list = new List&lt;int&gt;();\nlist.Add(1);\nlist.Add(2);\nlist.Add(3);\nlist.Convert(delegate (int i) { return i.ToString(); });\n</code></pre>\n<p>Something along those lines.</p>\n<hr />\n<p>Glenn's answer is probably the correct code ;-)</p>\n"}, {'answer_id': 44950, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 1, 'selected': False, 'text': "<p>You have to build a new list.  The underlying bit representations of <code>List&lt;int&gt;</code> and <code>List&lt;string&gt;</code> are completely incompatible -- on a 64-bit platform, for instance, the individual members aren't even the same size.</p>\n\n<p>It is theoretically possible to treat a <code>List&lt;string&gt;</code> as a <code>List&lt;object&gt;</code> -- this gets you into the exciting worlds of covariance and contravariance, and is not currently supported by C# or VB.NET.</p>\n"}, {'answer_id': 44951, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 3, 'selected': False, 'text': "<p>You wouldn't be able to directly cast it as no explicit or implicit cast exists from int to string, it would <em>have</em> to be a method involving .ToString() such as:-</p>\n\n<pre><code>foreach (int i in intList) stringList.Add(i.ToString());\n</code></pre>\n\n<p><strong>Edit</strong> - 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.</p>\n"}, {'answer_id': 44954, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 8, 'selected': True, 'text': '<p>.NET 2.0 has the <code>ConvertAll</code> method where you can pass in a converter function:</p>\n\n<pre><code>List&lt;int&gt;    l1 = new List&lt;int&gt;(new int[] { 1, 2, 3 } );\nList&lt;string&gt; l2 = l1.ConvertAll&lt;string&gt;(delegate(int i) { return i.ToString(); });\n</code></pre>\n'}, {'answer_id': 2367212, 'author': 'Luke', 'author_id': 14275, 'author_profile': 'https://Stackoverflow.com/users/14275', 'pm_score': 7, 'selected': False, 'text': '<p>Updated for 2010 </p>\n\n<pre><code>List&lt;int&gt; l1 = new List&lt;int&gt;(new int[] { 1,2,3 } );\nList&lt;string&gt; l2 = l1.ConvertAll&lt;string&gt;(x =&gt; x.ToString());\n</code></pre>\n'}, {'answer_id': 15177929, 'author': 'lutecki', 'author_id': 791252, 'author_profile': 'https://Stackoverflow.com/users/791252', 'pm_score': 3, 'selected': False, 'text': '<p>You can use:</p>\n\n<pre><code>List&lt;int&gt; items = new List&lt;int&gt;(new int[] { 1,2,3 } );\nList&lt;string&gt; s = (from i in items select i.ToString()).ToList();\n</code></pre>\n'}, {'answer_id': 44409533, 'author': 'Jayant Rajwani', 'author_id': 3801676, 'author_profile': 'https://Stackoverflow.com/users/3801676', 'pm_score': 2, 'selected': False, 'text': '<p><code>result = listOfInt.Select(i =&gt; i.ToString(CultureInfo.InvariantCulture)).ToList()</code></p>\n\n<p>replace the parameters result and listOfInt to your parameters</p>\n'}, {'answer_id': 63947737, 'author': 'Zuabros', 'author_id': 5045664, 'author_profile': 'https://Stackoverflow.com/users/5045664', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n<pre><code>var stringlist = intlist.Select(x=&gt;&quot;&quot;+x).ToList();\n</code></pre>\n<p>Or also:</p>\n<pre><code>var stringlist = intlist.Select(x=&gt;$&quot;{x}&quot;).ToList();\n</code></pre>\n<p>And finally the traditional:</p>\n<pre><code>var stringlist = intlist.Select(x=&gt;x.ToString()).ToList();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44942', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/493/']