Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>.NET 4+</strong></p> <pre><code>IList&lt;string&gt; strings = new List&lt;string&gt;{"1","2","testing"}; string joined = string.Join(",", strings); </code></pre> <p><strong>Detail &amp; Pre .Net 4.0 Solutions</strong></p> <p><code>IEnumerable&lt;string&gt;</code> can be converted into a string array <em>very</em> easily with LINQ (.NET 3.5):</p> <pre><code>IEnumerable&lt;string&gt; strings = ...; string[] array = strings.ToArray(); </code></pre> <p>It's easy enough to write the equivalent helper method if you need to:</p> <pre><code>public static T[] ToArray(IEnumerable&lt;T&gt; source) { return new List&lt;T&gt;(source).ToArray(); } </code></pre> <p>Then call it like this:</p> <pre><code>IEnumerable&lt;string&gt; strings = ...; string[] array = Helpers.ToArray(strings); </code></pre> <p>You can then call <code>string.Join</code>. Of course, you don't <em>have</em> to use a helper method:</p> <pre><code>// C# 3 and .NET 3.5 way: string joined = string.Join(",", strings.ToArray()); // C# 2 and .NET 2.0 way: string joined = string.Join(",", new List&lt;string&gt;(strings).ToArray()); </code></pre> <p>The latter is a bit of a mouthful though :)</p> <p>This is likely to be the simplest way to do it, and quite performant as well - there are other questions about exactly what the performance is like, including (but not limited to) <a href="https://stackoverflow.com/questions/219519/whats-the-c-method-syntax-for-converting-an-array-to-a-simple-string">this one</a>.</p> <p>As of .NET 4.0, there are more overloads available in <a href="http://msdn.microsoft.com/en-us/library/system.string.join" rel="noreferrer"><code>string.Join</code></a>, so you can actually just write:</p> <pre><code>string joined = string.Join(",", strings); </code></pre> <p>Much simpler :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload