退出 C# 中的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/699602/
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 13:58:05  来源:igfitidea点击:

Exiting a method in C#

c#methods

提问by

In C++ if I want to exit a method without returning anything I can do;

在 C++ 中,如果我想退出一个方法而不返回我可以做的任何事情;

// A completely useless method
static public double function(int a){
  if(a==1){
     cout << "Error !";
     exit(1);
  }
  else
     return (double)a;       
}

How can I do the equivalent in C# ?

如何在 C# 中执行等效操作?

回答by Paul Stovell

The "exit" function doesn't just exit the method - it terminates the program.

“退出”函数不只是退出方法 - 它终止程序。

http://msdn.microsoft.com/en-us/library/k9dcesdd.aspx

http://msdn.microsoft.com/en-us/library/k9dcesdd.aspx

In C# Console Applications, you could call System.Environment.Exit().

在 C# 控制台应用程序中,您可以调用 System.Environment.Exit()。

But I would suggest that any code doing this could be written with much better structure. For example, your method could throw an exception, and let the caller decide how to handle it.

但我建议这样做的任何代码都可以用更好的结构编写。例如,您的方法可能会抛出异常,并让调用者决定如何处理它。

回答by Paul Stovell

try return to exit out of the method without exiting the whole program.

尝试 return 退出方法而不退出整个程序。

回答by Ray

You can exit a method without returning anything by using the return statement. But if the return type is anything apart from void you will have to return something.

您可以使用 return 语句退出方法而不返回任何内容。但是如果返回类型是除了 void 之外的任何类型,你将不得不返回一些东西。

If you want to get out of a method because of an error, you should look at exceptions.

如果你想因为一个错误而退出一个方法,你应该查看异常。

回答by Mark Brackett

That would certainly be an odd thing to do, but yeah - you could call Environment.Exit. I'd stronglysuggest throwing an Exception instead though. That gets you a chance to cleanup, and your caller can decide how to handle the error. If nobody does, you'll still bring down the process - but somewhat more gracefully.

这当然是一件奇怪的事情,但是是的 - 你可以调用 Environment.Exit。不过,我强烈建议抛出异常。这让您有机会进行清理,并且您的调用者可以决定如何处理错误。如果没有人这样做,你仍然会降低这个过程——但会更优雅一些。

回答by Jon Limjap

Considering the context of what you want to do, this should be the equivalent:

考虑到你想要做的事情的上下文,这应该是等价的:

public static double SomeFunction(int a)
{
    if(a==1)
    {
        throw new Exception("Error!");
    }
    else
    {
        return (double)a;
    }
}

回答by Jon Limjap

You don't. By following your method, you'd have to do a test after every call to your function, to check if a double was in fact returned.

你没有。通过遵循您的方法,您必须在每次调用函数后进行测试,以检查实际上是否返回了双精度值。

In your example, you'd do something like...

在你的例子中,你会做这样的事情......

public static double myFunc( int a ) {

   if( a==1 ){
       throw new InvalidArgumentException( "a must be greater than one." );
   }

   return (double)a;

}

回答by Guffa

The usual way of handling an error condition in .NET is to throw an exception:

在 .NET 中处理错误条件的通常方法是抛出异常:

public static double function(int a) {
   if (a == 1) {
      throw new ArgumentException("The value 1 is not accepted.");
   }
   return (double)a;       
}

The exception would be caught by the code calling the method, or somewhere down the line. It's up to the calling code to handle it at an appropriate level.

异常将被调用该方法的代码捕获,或者被下线的某个地方捕获。由调用代码在适当的级别处理它。

It's quite usual for methods to sanitise the input in this manner, so that any faulty values are caught early instead of causing an error later in the code where it is much harder to track down.

以这种方式清理输入的方法很常见,以便及早捕获任何错误值,而不是稍后在代码中导致更难追踪的错误。

回答by Jhonny D. Cano -Leftware-

Another option taking into account all the previous answers, and in the case you are working with Windows Forms is to call

考虑到所有先前答案的另一种选择,如果您正在使用 Windows 窗体,则调用

Application.Exit();