What are you trying to achieve? Often I want to display the contents of a list, so I created the following extension method:
public static string Join(this IEnumerable<string> strings, string seperator) { return string.Join(seperator, strings.ToArray()); }
Then it is consumed as follows
var output = list.Select(a.ToString()).Join(",");
EDIT . To make it easier to use for lists without strings, here is another option above
public static String Join<T>(this IEnumerable<T> enumerable, string seperator) { var nullRepresentation = ""; var enumerableAsStrings = enumerable.Select(a => a == null ? nullRepresentation : a.ToString()).ToArray(); return string.Join(seperator, enumerableAsStrings); } public static String Join<T>(this IEnumerable<T> enumerable) { return enumerable.Join(","); }
Now you can use it like this
int[] list = {1,2,3,4}; Console.WriteLine(list.Join());
source share