andrewducker (
andrewducker) wrote2013-01-15 02:55 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Things I learned today about C#
The code I've been writing to convert a list into a comma-delimited string that consists of:
var duckerBrothers = new List<string> { "Andy", "Mike", "Hugh" };
var output = string.Empty;
foreach (var duckerBrother in duckerBrothers)
{
if (output != string.Empty)
{
output += ", ";
}
output += duckerBrother;
}
Can be rewritten as:
var duckerBrothers = new List<string> { "Andy", "Mike", "Hugh" };
var output = string.Join(",", duckerBrothers);
That's what I get for assuming that there is no built-in way of doing things.
var duckerBrothers = new List<string> { "Andy", "Mike", "Hugh" };
var output = string.Empty;
foreach (var duckerBrother in duckerBrothers)
{
if (output != string.Empty)
{
output += ", ";
}
output += duckerBrother;
}
Can be rewritten as:
var duckerBrothers = new List<string> { "Andy", "Mike", "Hugh" };
var output = string.Join(",", duckerBrothers);
That's what I get for assuming that there is no built-in way of doing things.
no subject
Actually, wouldn't your way, rather than the built-in way, be better if you wanted to replace the final ", " with " and "?
no subject
no subject
It's not quite as neat as Python, certainly!
no subject
brothersDucker
?no subject
no subject