I have code that I want to make the following changes:
How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)\'
What are you trying to achieve? Often I want to output the contents of a list, so I created the following extension method:
public static string Join(this IEnumerable strings, string seperator)
{
return string.Join(seperator, strings.ToArray());
}
It is then consumed like this
var output = list.Select(a.ToString()).Join(",");
EDIT: To make it easier to use for non string lists, here is another variation of above
public static String Join(this IEnumerable 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(this IEnumerable enumerable)
{
return enumerable.Join(",");
}
Now you can consume it like this
int[] list = {1,2,3,4};
Console.WriteLine(list.Join()); // 1,2,3,4
Console.WriteLine(list.Join(", ")); // 1, 2, 3, 4
Console.WriteLine(list.Select(a=>a+".0").Join()); // 1.0, 2.0, 3.0, 4.0