Alex Rivera | Logout

How to cancel the execution of a method?

Asked 2009-04-13T14:18:47.297
15

Consider i execute a method 'Method1' in C#. Once the execution goes into the method i check few condition and if any of them is false, then the execution of Method1 should be stopped. how can i do this, i.e can the execution of a method when certain conditions are met.?

but my code is something like this,

int Method1()
{
    switch(exp)
    {
        case 1:
        if(condition)
            //do the following. **
        else
            //Stop executing the method.**
        break;
        case2:
        ...
    }
}
c#
Edit
Report

1 Answer

1

You could setup a guard clause with a return statement:

public void Method1(){

 bool isOK = false;

 if(!isOK) return; // <- guard clause

 // code here will not execute...


}
answered 2009-04-13T14:21:21.113

Your Answer