C# 如何取消一个方法的执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/744013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:28:45  来源:igfitidea点击:

How to cancel the execution of a method?

c#

提问by SyncMaster

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

考虑我在 C# 中执行一个方法“Method1”。一旦执行进入方法,我会检查几个条件,如果其中任何一个为假,则应停止 Method1 的执行。我怎样才能做到这一点,即可以在满足某些条件时执行方法。?

but my code is something like this,

但我的代码是这样的,

int Method1()
{
    switch(exp)
    {
        case 1:
        if(condition)
            //do the following. **
        else
            //Stop executing the method.**
        break;
        case2:
        ...
    }
}

采纳答案by Mehrdad Afshari

Use the returnstatement.

使用return语句。

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

// body...

回答by Jesper Fyhr Knudsen

I think this is what you are looking for.

我想这就是你要找的。

if( myCondition || !myOtherCondition )
    return;

Hope it answered your question.

希望它回答了你的问题。

Edit:

编辑:

If you want to exit the method due to an error you can throw an exception like this:

如果由于错误而想退出该方法,则可以抛出如下异常:

throw new Exception( "My error message" ); 

If you want to return with a value, you should return like before with the value you want:

如果你想返回一个值,你应该像以前一样返回你想要的值:

return 0;

If it is the Exception you need you can catch it with a try catch in the method calling your method, for instance:

如果它是您需要的异常,您可以在调用您的方法的方法中使用 try catch 捕获它,例如:

void method1()
{
    try
    {
        method2( 1 );
    }
    catch( MyCustomException e )
    {
        // put error handling here
    }

 }

int method2( int val )
{
    if( val == 1 )
       throw new MyCustomException( "my exception" );

    return val;
}

MyCustomException inherits from the Exception class.

MyCustomException 继承自 Exception 类。

回答by Codebrain

You could setup a guard clause with a return statement:

您可以使用 return 语句设置保护子句:

public void Method1(){

 bool isOK = false;

 if(!isOK) return; // <- guard clause

 // code here will not execute...


}

回答by Otávio Décio

There are a few ways to do that. You can use returnor throwdepending if you consider it an error or not.

有几种方法可以做到这一点。您可以使用returnthrow取决于您是否认为它是错误的。

回答by DevinB

Are you talking about multi threading?

你说的是多线程吗?

or something like

或类似的东西

int method1(int inputvalue)
{
   /* checking conditions */
   if(inputvalue < 20)
   {
      //This moves the execution back to the calling function
      return 0; 
   }
   if(inputvalue > 100)
   {
      //This 'throws' an error, which could stop execution in the calling function.
      throw new ArgumentOutOfRangeException(); 
   }
   //otherwise, continue executing in method1

   /* ... do stuff ... */

   return returnValue;
}