How to cancel the execution of a method?

后端 未结 5 749
眼角桃花
眼角桃花 2020-12-30 08:18

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 shou

5条回答
  •  时光说笑
    2020-12-30 08:47

    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...
    
    
    }
    

提交回复
热议问题