How do I exit a foreach loop in C#?

后端 未结 5 1862
温柔的废话
温柔的废话 2020-12-09 14:36
foreach (var name in parent.names)
{
    if name.lastname == null)
    {
        Violated = true;
        this.message = \"lastname reqd\";
    }

    if (!Violated)         


        
5条回答
  •  情话喂你
    2020-12-09 15:04

    Look at this code, it can help you to get out of the loop fast!

    foreach (var name in parent.names)
    {
       if (name.lastname == null)
       {
          Violated = true;
          this.message = "lastname reqd";
          break;
       }
       else if (name.firstname == null)
       {
          Violated = true;
          this.message = "firstname reqd";
          break;
       }
    }
    

提交回复
热议问题