Is there an elegant way to repeat an action?

后端 未结 9 1307
野的像风
野的像风 2020-12-16 08:56

In C#, using .NET Framework 4, is there an elegant way to repeat the same action a determined number of times? For example, instead of:

int repeat = 10;
for          


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 09:49

    Table table = frame.AddTable();
    int columnsCount = 7;
    
    Enumerable.Repeat>(table.AddColumn, columnsCount)
              .ToList()
              .ForEach(addColumn => addColumn());
    //or
    Enumerable.Range(0, columnsCount)
              .ToList()
              .ForEach(iteration => table.AddColumn());
    

    these options are not elegant because of ToList(), but both worked in my case

提交回复
热议问题