C# 摆脱无效方法?

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

Get out of a void method?

c#coding-style

提问by

I have this method (modified code) :

我有这个方法(修改后的代码):

public static void PublishXmlForCustomTypes(MyOwnClass DefaultOutputInformation)
{
    if (DefaultOutputInformation != null)
    {
        ///lot of code
    }
}

and my whole code was inside the if statement and after thinking about it, I changed to this :

我的整个代码都在 if 语句中,经过思考,我改为:

public static void PublishXmlForCustomTypes(MyOwnClass DefaultOutputInformation)
{
    if (DefaultOutputInformation == null)
    {
        return;
    }
    ///lot of code
}

As far as I tested it, it seems to be strictly equivalent but is that really the case ? I mean, the "return" statement get us out of the method right ?

就我测试而言,它似乎严格等效,但真的是这样吗?我的意思是,“return”语句让我们脱离了方法,对吗?

采纳答案by Guillaume Slashy

This is strictly equivalent and the second version is the way to go :)

这是严格等效的,第二个版本是要走的路:)

回答by leppie

Yes, your assumptions is correct.

是的,你的假设是正确的。

For some background, learn about duality.

对于一些背景,了解对偶性

回答by leppie

Yes, it is exactly the same, you can read the MSDN documentation about the keyword return to fully understand how it works : http://msdn.microsoft.com/en-us/library/1h3swy84.aspx

是的,完全一样,您可以阅读有关关键字 return 的 MSDN 文档以完全了解其工作原理:http: //msdn.microsoft.com/en-us/library/1h3swy84.aspx

As to decide which way is better : both are good, but the second version makes it more readable because then your whole code isn't inside an if block. This way, you can see what the condition does really easily instead of reading the whole code of the method.

至于决定哪种方式更好:两者都很好,但是第二个版本使其更具可读性,因为您的整个代码不在 if 块中。这样,您可以很容易地看到条件做了什么,而不是阅读方法的整个代码。

回答by Davide Piras

yes returngets you out of the method; if you have a finallyblock and you call return from the tryblock, the finallyblock is executed anyway.

是的,return让你摆脱这种方法;如果您有一个finally块并且您从该try块中调用 return ,finally则无论如何都会执行该块。

回答by Jon Skeet

Yes, that's absolutely fine.

是的,那绝对没问题。

Some people dogmatically stick to "one exit point per method" - which was appropriate when it was relatively tricky to make sure you always did the right amount of clean-up at the end of a function in C, for example... but it's not really necessary in C#.

有些人教条地坚持“每个方法有一个退出点”——例如,当确保在 C 中的函数结束时始终进行适量的清理相对困难时,这是合适的……但它是在 C# 中并不是真正必要的。

Personally I think it's appropriate to return as soon as you know that you've done all the work you really want to in a method. Use try/finallyor usingstatements to perform any extra "clean up however I exit" work.

我个人认为,一旦您知道您已经在方法中完成了您真正想要的所有工作,就立即返回是合适的。使用try/finallyorusing语句执行任何额外的“清理但我退出”工作。

回答by Grizzly

Indeed the returngets you out of the method, so it is equivalent to the first way you used. Which way is better depends on your code, although generally I would prefer the second version.

确实使return您摆脱了该方法,因此它等效于您使用的第一种方式。哪种方式更好取决于您的代码,尽管通常我更喜欢第二个版本。

回答by Jorge

Yes, the returnstatement ends the method.

是的,return语句结束了该方法。

回答by FuriousGeorge

Yes, the return will exit you out of the code. It's generally good practice as the very first step in a function to verify that the parameters that were passed in are what you think they are and exit (via the return or throwing an exception) so that you don't do any unnecessary processing only to have to abort later in the function.

是的,返回将使您退出代码。作为函数中的第一步,验证传入的参数是否与您认为的一致并退出(通过返回或抛出异常)通常是一种很好的做法,这样您就不会只执行任何不必要的处理必须稍后在函数中中止。

回答by FuriousGeorge

Looking at the revised code, the second one is the way to go. While being functionally equivalent, think about the case where you passed in 4 different variables to a function that you want to check. Instead of having a do a nasty 4 level if statement with {'s everywhere, the second method allows you to clean up the appearance of the code and not add unnecessary levels of brackets. If you're writing in C/C++, you can even make this a macro such as VERYIFY_NOT_NULL(x) and make the code nice and neat.

查看修改后的代码,第二个是要走的路。在功能上等效的同时,请考虑将 4 个不同的变量传递给要检查的函数的情况。第二种方法可以让您清理代码的外观,而不是添加不必要的括号级别,而不是使用 { 到处都是令人讨厌的 4 级 if 语句。如果您使用 C/C++ 编写,您甚至可以将其设为诸如 VERYIFY_NOT_NULL(x) 之类的宏,并使代码简洁明了。

Readable/maintainable code trumps nano-seconds of performance 99% of the time.

可读/可维护的代码在 99% 的情况下胜过纳秒级的性能。