Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ToArray() used when using string.Join with a List<string>?

Tags:

c#

I'm trying to combine List<string> strings using string.Join(",", strings) but everything I'm reading says I should do:

string.Join(",", strings.ToArray())

Is there a certain reason I have to/should use .ToArray()?

like image 629
strongriley Avatar asked Apr 10 '12 16:04

strongriley


2 Answers

string.Join only started accepting IEnumerable<string> (and indeed a generic overload) as of .NET 4. Presumably you're looking at code (or instructions) written with .NET 3.5 or earlier in mind. Compare the overloads:

  • string.Join in .NET 3.5
  • string.Join in .NET 4
like image 153
Jon Skeet Avatar answered Oct 20 '22 11:10

Jon Skeet


Probably because everything you're reading was written for an earlier version of the framework. The string.Join(string, IEnumerable<string>) method was added in version 4.

like image 30
phoog Avatar answered Oct 20 '22 11:10

phoog