c# - 如何取消方法的执行?

标签 c#

假设我在 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/

相关文章:

c# - 对 Linq 和分组感到困惑

c# - EF延迟加载返回null

c# - 如何在 Debug模式下关闭 Visual Studio 中的 DocFX,同时在 Release模式下处于事件状态?

c# - 将图像发送到我的 C# 应用程序

c# - Visual Studio 可扩展性 - 每个 Visual Studio 版本的不同 MEF 导出?

c# - 您什么时候决定将大型项目拆分成较小的项目?

c# - 超时模式——Thread.Abort 到底有多糟糕?

c# - 为什么 c# 不会仅仅因为 xs :choice in the schema? 而在 visual studio 中使用按服务引用添加从 wsdl 创建代码

c# - 通过 Java(或 C#)发送 MAPI 电子邮件

C# 分部类和 get 访问器