How to create dynamic incrementing variable using “for” loop in C#

后端 未结 6 613
南方客
南方客 2021-01-07 03:42

How to create dynamic incrementing variable using \"for\" loop in C#? like this: track_1, track_2, track_3, track_4. so on.

6条回答
  •  广开言路
    2021-01-07 04:03

    don't know if I get your question, but I will try:

    for(var i = 1; i < yourExclusiveUpperbound; i++)
    {
       var track = String.Format("$track_{0}", i);
       // use track
    }
    

    or with some LINQ-Magic:

    foreach(var track in Enumerate.Range(1, count)
                                  .Select(i => String.Format("$track_{0}", i)))
    {
       // use track
    }
    

提交回复
热议问题