Restart a foreach loop in C#?

前端 未结 5 1275
傲寒
傲寒 2021-02-07 04:02

How can I restart a foreach loop in C#??

For example:

Action a;
foreach(Constrain c in Constrains)
{
   if(!c.Allows(a))
   {
      a.Change         


        
5条回答
  •  野的像风
    2021-02-07 04:22

    void Main()
    {
        IEnumerable cons;
        SomeObject a;
    
        while(!TryChangeList(cons, a)) { }
    }
    
    // the name tryChangeList reveals the intent that the list will be changed
    private bool TryChangeList(IEnumerable constrains, SomeObject a)
    {
        foreach(var con in constrains)
        {
            if(!c.Allows(a))
            {
                a.Change();
                return false;
            }
        }
        return true;
    }
    

提交回复
热议问题