I have the following extension method that takes a List and converts it to a comma separated string:
static public string ToCsv(this List<string> lst)
{
const string SEPARATOR = ", ";
string csv = string.Empty;
foreach (var item in lst)
csv += item + SEPARATOR;
// remove the trailing separator
if (csv.Length > 0)
csv = csv.Remove(csv.Length - SEPARATOR.Length);
return csv;
}
I want to do something analogous but apply it to a List (instead of List of String) , however, the compiler can't resolve for T:
static public string ToCsv(this List<T> lst)
{
const string SEPARATOR = ", ";
string csv = string.Empty;
foreach (var item in lst)
csv += item.ToString() + SEPARATOR;
// remove the trailing separator
if (csv.Length > 0)
csv = csv.Remove(csv.Length - SEPARATOR.Length);
return csv;
}
What am I missing?