在调用对象的方法之前将对象存储在变量中是否有好处?

发布于 2024-09-06 09:07:17 字数 292 浏览 19 评论 0原文

示例 1:

SomeObject someObject = new SomeObject();
if (someObject.Method())
{
    //do stuff
}
//someObject is never used again

示例 2:

if (new SomeObject().Method())
{
    //do stuff
}

使用第一种方法相对于第二种方法有什么好处,反之亦然?

Example 1:

SomeObject someObject = new SomeObject();
if (someObject.Method())
{
    //do stuff
}
//someObject is never used again

vs

Example 2:

if (new SomeObject().Method())
{
    //do stuff
}

Is there any benefit to using the first method over the second, or vice versa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的背包 2024-09-13 09:07:17

至少有三个潜在的好处:

  1. 可读性:在许多情况下,第一个比第二个示例的语法更明显,尤其是对于新开发人员。

  2. 更好的调试体验:如果 SomeObject 的构造函数抛出异常,在第一种情况下,调试器将在该行中断。在第二种情况下,异常是在构造函数中还是在方法中并不明显。设置断点和检查对象上的值也会出现同样的问题 - 在第二种情况下这会很困难,并且需要在方法内部设置断点。

  3. 在第一种情况下,您可以在该单个调用之外使用该对象。如果您确实只需要单次调用的方法,并且不需要对象引用,那么静态方法可能更合适。

There are at least three potential benefits:

  1. Readability: the first is more obvious in many cases than the syntax of the second example, especially to newer developers.

  2. Better debugging experience: If the constructor for SomeObject throws an exception, in the first case, the debugger will break on that line. In the second case, it's not obvious whether the exception is in the constructor or the method. The same issue arises for setting break points and inspecting values on the object - this will be difficult in the second case, and require setting the break point inside of the method.

  3. In the first case, you can use the object outside of that single call. If you really only need a method for a single call, and don't need the object reference, then a static method may be more appropriate.

盛夏已如深秋| 2024-09-13 09:07:17

在大多数微不足道的情况下,编译器无论如何都会将其删除。值类型有一个重要的点,即将其复制到变量中会克隆该值,因此可以代表重大更改 - 但无论如何您都不应该鼓励可变值类型。

对于浮点,存在一些边缘情况,当它使用本地时,您会得到不同的答案(本机类型的宽度比 Single / Double 等更大) - 其中也意味着您可以通过调试/发布获得不同的结果(取决于编译器是否删除变量)。

在某些情况下,变量可以执行比堆栈上的值更多的操作 - 即被“捕获”到 lambda / 匿名方法中,或者用于 out/ref,但这很少适用。

In most trivial cases, the compiler will remove it anyway. There is an important point with value-types, which is that copying it into a variable clones the value, so can represent a significant change - but you shouldn't encourage mutable value-types anyway.

With floating-point, there are some edge-cases where when it uses a local you get different answers (the native types have greater width than Single / Double etc) - which also means you can get different results with debug/release (depending on whether the variable is removed by the compiler).

There are also cases where the variable can do more than the value on the stack - i.e. be "captured" into a lambda / anon-method, or be used for out/ref, but that rarely applies.

绝影如岚 2024-09-13 09:07:17

在调试时可以很有用,以便能够看到该值(如果该对象是 COM 互操作中的某个对象或类似的对象,您需要对其进行处理,那么保留引用可能很重要,以便您能够在受控的环境中执行此操作方式)。

如果名称很长或者有多层嵌套,也可以更容易阅读。

Can be useful while debugging to be able to see the value (and if the object was something in COM interop or similar that you'd need to dispose of it might be important to keep the reference so you are able to do so in a controlled manner).

If the names are long or if there are multiple levels of nesting it can also be easier to read.

晌融 2024-09-13 09:07:17

我倾向于第一个选项以提高可读性,例如:

var phoneNumberRx = new Regex(@"^\(\d{3}\)\d{3}-\d{4}$");

I'd favor the first option for improved readability, e.g.:

var phoneNumberRx = new Regex(@"^\(\d{3}\)\d{3}-\d{4}$");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文