Things I learned today about C#
Jan. 15th, 2013 02:55 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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
Date: 2013-01-15 03:11 pm (UTC)no subject
Date: 2013-01-15 03:15 pm (UTC)no subject
Date: 2013-01-15 03:59 pm (UTC)Actually, wouldn't your way, rather than the built-in way, be better if you wanted to replace the final ", " with " and "?
no subject
Date: 2013-01-15 04:07 pm (UTC)no subject
Date: 2013-01-15 10:14 pm (UTC)It's not quite as neat as Python, certainly!
no subject
Date: 2013-01-15 07:04 pm (UTC)brothersDucker
?no subject
Date: 2013-01-15 07:13 pm (UTC)no subject
Date: 2013-01-15 10:06 pm (UTC)