You can use the function for this String.Join(string glue, string[] array);.
string values = String.Join(", ", myList.ToArray());
Hope this helps. (Note: myListis a list that you already have in your application.)
Update: your noticed and the requirement is too late, my applications, here is a small update:
string values = String.Join(", ", myList.Take(myList.Count - 1))
+ " and " + myList.Last();
(, , ) StringBuilder . (: , .)
: .
public static string SmartJoin(this List<string> items, string lastSeparator)
{
string values = "";
if(!items.Any())
{
return "";
}
if (items.Count > 1)
{
values = String.Format("{0} {1} {2}",
String.Join(", ", items.Take(items.Count - 1)),
lastSeparator,
items.Last());
}
else
{
values = items.First();
}
return values;
}
string values = myList.SmartJoin("and");