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

  • instagram cross-post

    Jane is having a very well deserved nap, and so I am, for once, colonised by the livestock. Original is here on instagram. Original post…

  • Interesting Links for 28-02-2021

    Single Covid case puts New Zealand's largest city Auckland back into lockdown (tags: pandemic newzealand ) Do you like it when things in your…

  • instagram cross-post

    Sophia (middle of shot, hurtling into the distance) has just noticed the return of the ice cream van. I swear you could hear her excited yell…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 31 comments

  • instagram cross-post

    Jane is having a very well deserved nap, and so I am, for once, colonised by the livestock. Original is here on instagram. Original post…

  • Interesting Links for 28-02-2021

    Single Covid case puts New Zealand's largest city Auckland back into lockdown (tags: pandemic newzealand ) Do you like it when things in your…

  • instagram cross-post

    Sophia (middle of shot, hurtling into the distance) has just noticed the return of the ice cream van. I swear you could hear her excited yell…