假设我在 C# 中执行方法“Method1”。 一旦执行进入方法,我会检查几个条件,如果其中任何条件为假,则应停止执行 Method1。我该怎么做,即可以在满足特定条件时执行方法。?
但是我的代码是这样的,
int Method1()
{
switch(exp)
{
case 1:
if(condition)
//do the following. **
else
//Stop executing the method.**
break;
case2:
...
}
}
最佳答案
使用return
语句。
if(!condition1) return;
if(!condition2) return;
// body...
关于c# - 如何取消方法的执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/744013/