从另一个方法停止执行该方法

Stop the execution of the method from another method

请告诉我如何停止从 CheckStr() 执行 Script()

例子

public void Script()
{
// ...
    string str = "error";
    CheckStr(str);
// ...
}
public void CheckStr(string str)
{
    if (str == "error")
    {
         // stop Script();
    }
}

你可以让 CheckStr() return 一个值:

public void Script()
{
    string str = "error";
    if (!CheckStr(str))
    {
        return;
    }

    // ...continue
}

public bool CheckStr(string str)
{
    if (str == "error")
    {
        return false;
    }

    // ...additional checks

    return true;
}

最简单的方法是得到 CheckStr return 个结果,例如true/false

public bool CheckStr(string str)
{
    if (str == "error")
    {
        return false;
    }
    ...
    return true;
}

public void Script()
{
    // ...
    string str = "error";
    if (CheckStr(str) == false)
    {
        return;
    }
    // ...
}

您可以从 CheckStr 中抛出异常,但我不确定这是否能解决您的特定问题:

public void CheckStr(string str)
{
    if (str == "error")
    {
         throw new Exception();
    }
}

然后您可以在 Script 或其他地方捕捉它:

public void Script()
{
// ...
    string str = "error";
    try {
       CheckStr(str);
    }
    catch
    {
        // handle excpetion here.
    }
// ...
}