Other ways to deal with “loop initialization” in C#

前端 未结 9 722
陌清茗
陌清茗 2020-12-24 07:22

To start with I\'ll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn\'t be used when

9条回答
  •  时光说笑
    2020-12-24 08:04

    Sometimes I use LINQ .First() and .Skip(1) to handle this ... This can give a relatively clean (and very readable) solution.

    Using you example,

    append(array.First());
    foreach(var x in array.Skip(1))
    {
      append(delimiter);
      append (x);
    }
    

    [This assumes there is at least one element in the array, an easy test to add if that's to be avoided.]

    Use F# would be another suggestion :-)

提交回复
热议问题