Tuesday, May 24, 2011

IEnumerable(T) != IEnumerable

String.Join(String, IEnumerable(of T)) was added in .NET 4.0 as a new overloaded method that takes a strongly-typed enumeration and returns a separated string. This is great and something we had previously done in a wrapper function. However, one thing to be careful here is that passing in an ArrayList will not throw an exception (compile or runtime) but may not give you the results you are expecting.
For example the following code:
ArrayList array = new ArrayList() {1, 2, 3};
string joinedString = String.Join(",", array);
Console.Write(joinedString);

Will write out:
System.Collections.ArrayList

Not:
1, 2, 3

No comments: