Is it possible to write looping system with syntax like while or for or foreach? RRS feed

  • Question

  • Recently I've written my own looping system and currently it being called looks like this:

    MyLoopingSystem(ThreadCount,SomeList,loopVar => {/*commands go here*/});

    and it works fine but doesnt really look nice and I was wondering is it possible to make it look more like for,while or foreach, all arguments go into the bracket and than the method that it is suppose to be executed goes into curly brackets afterwards.

    Sunday, September 8, 2019 2:08 PM


  • No, you can't really make it look like an ordinary loop. For inspiration, you can take a look at how the developers from Microsoft implemented the Parallel.ForEach loop. This does exactly the same thing that you are attempting to do, but they have to pass the code to be looped as a lambda expression, just the same as you are doing in your looping system. If there was a better way to do it, rest assured that the developers of Parallel.For and Parallel.Foreach would have used it for the Parallel library.
    • Marked as answer by BataBo Jokviu Sunday, September 8, 2019 5:16 PM
    Sunday, September 8, 2019 3:23 PM