如何取消方法的执行? [英] How to cancel the execution of a method?

查看:117
本文介绍了如何取消方法的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我在C#中执行的方法方法一。
一旦执行进入我检查几个条件,如果其中的任何是假的,那么方法1的执行应停止方法。我怎么能做到这一点,即可以在方法的执行,当某些条件得到满足?



但我的代码是这样的,

  INT方法1()
{
开关(EXP)
{
案例1:
如果(条件)
//执行以下操作
**别的
//停止执行方法**
案例2:
...
}
}


解决方案

使用<$ C (!条件1)$ C>收益语句

 如果回报; 
如果回报(条件2!);

//身体... ...


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.**
case2:
...
}
}

解决方案

Use the return statement.

if(!condition1) return;
if(!condition2) return;

// body...

这篇关于如何取消方法的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆