convert array of objects to concatenated string
Posted
by oo
on Stack Overflow
See other posts from Stack Overflow
or by oo
Published on 2010-05-12T12:29:31Z
Indexed on
2010/05/12
12:34 UTC
Read the original article
Hit count: 237
if i have:
List<Car>
where car is:
public class Car
{
public int Year;
public string Name;
}
and i want to take this array and create a concatenated string by ","
so it would return:
"Toyota", "Ford", "Chevy"
i can do it manually like this:
private static string CreateConcatenatedList(List<Car> parts_)
{
StringBuilder b = new StringBuilder();
foreach (Car bp in parts_)
{
b.Append(bp.Name + ", ");
}
b.Remove(b.Length - 2, 2);
return b.ToString();
}
but i thought there might be a more elegant way
© Stack Overflow or respective owner