c# - Creating a comma separated list from IList<string> or IEnumerable<string> -


a IList < String & gt; The most obvious way to create a comma-separated list of string values ​​is by or IEnumerable & lt; String & gt; ?

String.Join (...) operates on a string [] so when IList & lt; String & gt; or IEnumerable & lt; String & gt; String can not be easily changed in the array such as , it can be cumbersome to work with.

.NET 4 +

  IILIT & lt; String & gt; Wire = new list & lt; String & gt; {"1", "2", "test"}; String included = string .join (",", wire);  

Extensions & amp; Former .NET 4.0 solutions

IEnumerable & lt; String & gt; can be easily converted into too many string arrays with LINQ (.NET 3.5). :

IEnumerable & lt; String & gt; Wire = ...; String [] array = string Toure ();

If you need to, then it is quite easy to write equivalent support methods:

  public static t [] ToArray (IEnumerable & lt; T & gt; Source) {return new list & lt; T & gt; (Source) .ToArray (); }  

Then call it like this:

  IEnumerable & lt; String & gt; Wire = ...; String [] array = assistant .oir (wire);  

You can then call the string. Of course, you do not have not to use a helpful method:

  // C # 3 and .NET 3.5 ways: include string = string .Join (",", string. Tauer ()); // like C # 2 and .NET 2.0: include string = string.Join (",", new list & lt; string & gt; (strings) .oir ());  

The latter is a little bit of a whisper :)

It can be the easiest way to do it, and it's good enough - Not limited to), absolutely the question about. (But not limited to)

In the form of .NET 4.0, more surcharges are available, so that you can actually just write:

  join the string = string.Join ("," , Wire); Very simple :) 


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -