c# stop code execution within library
<p>I'm trying to figure out in C# Winforms if is anywhere possible to stop code execution by calling a function and not a <code>return</code>.</p>

<p>The following code is possible in PHP</p>

<pre><code>if($something == null)
   $this-&gt;response-&gt;error(0);

// This code is never executed if the condition is true
echo 'Hello';
</code></pre>

<p>And the library <code>response</code>, has something like:</p>

<pre><code>public class Response
{
   public function error($index)
   {
     $response = array();

     switch ($index)
     {
        case 0: $response = array('msg' =&gt; 'fields missing..'); break;
     }

     // The trick is here
     exit(json_encode($response));
   }
}
</code></pre>

<p>So, in the C# project and within my form I call the <code>response</code> library this way:</p>

<pre><code>private void button1_Click(object sender, EventArgs e)
{
    libraries.Response Response = new libraries.Response();

    if(textBox1.Text == "")
        Response.error(0);

    // The code continues to get executed even if the condition is true
    button2.PerformClick();
}
</code></pre>

<p>The response triggers an <code>MessageBox</code> which obviously has nothing that make code execution to stop.</p>

<pre><code>class Response
{
    public void error(int index)
    { 
        string msg = "";

        switch (index) 
        {
            case 0: msg = "Fields missing.."; break;
        }

        MessageBox.Show(msg, "My app", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
</code></pre>

<p>Now, I know that I can use the <code>return</code> as the below code shows to stop code execution, but I was wondering if there's something else that I can put in the library <code>response</code>that would do the trick?</p>

<pre><code>if(textBox1.Text == "")
{
   Response.error(0);
   return;
}
</code></pre>