Given:
protected class Marker {
public string Name { get; set; }
public string Lat { get; set; }
public string Long { get; set; }
};
List<Marker> allMarkers = new List<Marker>();
allMarkers.Add(new Marker{Name="Bondi Beach", Lat = "-33.890542", Long = "151.274856"});
allMarkers.Add(new Marker{Name="Coogee Beach", Lat = "-33.923036", Long = "151.259052"});
allMarkers.Add(new Marker{Name="Cronulla Beach", Lat = "-34.028249", Long = "151.157507"});
allMarkers.Add(new Marker{Name="Manly Beach", Lat = "-33.800101", Long = "151.287478"});
allMarkers.Add(new Marker{Name="Maroubra Beach", Lat = "-33.950198", Long = "151.259302"});
I’d like to convert to a string in this format:
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.800101, 151.287478],
['Maroubra Beach', -33.950198, 151.259302]
Is there a one liner way to do this, something similar to string.Join(), or do I have to do it manually via a foreach on the List and use stringbuilder.appendformat()?
If
Markeris your own class, consider overriding theToString()method to display each line the way you do. Then, you can use a simpleString.Join()to combine it all together.Then, to use:
Note: If you are dealing with a lot of markers and notice bad performance, consider rewriting that
String.Format()line as:You may notice this is better (or worse), depending on your use case.