Andrew Ducker (andrewducker) wrote,
Andrew Ducker
andrewducker

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.



Original post on Dreamwidth - there are comment count unavailable comments there.
Subscribe

  • Interesting Links for 17-09-2021

    Tesla autopilot will randomly swerve towards pedestrians (tags: Tesla automation driving murder epicfail video ) The Fellowship of the Walk…

  • Interesting Links for 16-09-2021

    On doing your own vaccine research (tags: vaccine research comic ) Who talks more in your meetings - men or women? (tags: talking gender…

  • Interesting Links for 15-09-2021

    The NHS in Scotland: Your gran might be lying on the floor for 10 hours (tags: scotland nhs pandemic doom ) Edinburgh Council are having a…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 31 comments

  • Interesting Links for 17-09-2021

    Tesla autopilot will randomly swerve towards pedestrians (tags: Tesla automation driving murder epicfail video ) The Fellowship of the Walk…

  • Interesting Links for 16-09-2021

    On doing your own vaccine research (tags: vaccine research comic ) Who talks more in your meetings - men or women? (tags: talking gender…

  • Interesting Links for 15-09-2021

    The NHS in Scotland: Your gran might be lying on the floor for 10 hours (tags: scotland nhs pandemic doom ) Edinburgh Council are having a…