返回值或输出参数哪个更好? [英] Which is better, return value or out parameter?

查看:30
本文介绍了返回值或输出参数哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们想从一个方法中获取一个值,我们可以使用任一返回值,就像这样:

If we want to get a value from a method, we can use either return value, like this:

public int GetValue(); 

或:

public void GetValue(out int x);

我不太了解它们之间的区别,因此,不知道哪个更好.你能解释一下吗?

I don't really understand the differences between them, and so, don't know which is better. Can you explain me this?

谢谢.

推荐答案

返回值几乎总是在方法没有其他任何返回值时是正确的选择.(事实上​​,如果我可以选择的话,我想不出任何我曾经想要一个带有 out 参数的 void 方法的情况.C# 7 的 Deconstruct 语言支持的解构方法是这条规则的一个非常非常罕见的例外.)

Return values are almost always the right choice when the method doesn't have anything else to return. (In fact, I can't think of any cases where I'd ever want a void method with an out parameter, if I had the choice. C# 7's Deconstruct methods for language-supported deconstruction acts as a very, very rare exception to this rule.)

除此之外,它使调用者不必单独声明变量:

Aside from anything else, it stops the caller from having to declare the variable separately:

int foo;
GetValue(out foo);

对比

int foo = GetValue();

输出值也可以防止方法链像这样:

Out values also prevent method chaining like this:

Console.WriteLine(GetValue().ToString("g"));

(实际上,这也是属性设置器的问题之一,这也是构建器模式使用返回构建器的方法的原因,例如 myStringBuilder.Append(xxx).Append(yyy).)

(Indeed, that's one of the problems with property setters as well, and it's why the builder pattern uses methods which return the builder, e.g. myStringBuilder.Append(xxx).Append(yyy).)

此外,输出参数与反射一起使用稍微困难一些,并且通常也会使测试变得更加困难.(通常会付出更多的努力来使模拟返回值比输出参数更容易).基本上没有什么我能想到的,它们让更容易...

Additionally, out parameters are slightly harder to use with reflection and usually make testing harder too. (More effort is usually put into making it easy to mock return values than out parameters). Basically there's nothing I can think of that they make easier...

返回值 FTW.

就正在发生的事情而言......

In terms of what's going on...

基本上,当你为out"参数传入一个参数时,你必须传入一个变量.(数组元素也被归类为变量.)您调用的方法在其堆栈中没有用于参数的新"变量 - 它使用您的变量进行存储.变量中的任何更改都立即可见.这是一个显示差异的示例:

Basically when you pass in an argument for an "out" parameter, you have to pass in a variable. (Array elements are classified as variables too.) The method you call doesn't have a "new" variable on its stack for the parameter - it uses your variable for storage. Any changes in the variable are immediately visible. Here's an example showing the difference:

using System;

class Test
{
    static int value;

    static void ShowValue(string description)
    {
        Console.WriteLine(description + value);
    }

    static void Main()
    {
        Console.WriteLine("Return value test...");
        value = 5;
        value = ReturnValue();
        ShowValue("Value after ReturnValue(): ");

        value = 5;
        Console.WriteLine("Out parameter test...");
        OutParameter(out value);
        ShowValue("Value after OutParameter(): ");
    }

    static int ReturnValue()
    {
        ShowValue("ReturnValue (pre): ");
        int tmp = 10;
        ShowValue("ReturnValue (post): ");
        return tmp;
    }

    static void OutParameter(out int tmp)
    {
        ShowValue("OutParameter (pre): ");
        tmp = 10;
        ShowValue("OutParameter (post): ");
    }
}

结果:

Return value test...
ReturnValue (pre): 5
ReturnValue (post): 5
Value after ReturnValue(): 10
Out parameter test...
OutParameter (pre): 5
OutParameter (post): 10
Value after OutParameter(): 10

区别在于post"步骤 - 即在局部变量或参数被更改之后.在 ReturnValue 测试中,这与静态 value 变量没有区别.在 OutParameter 测试中,value 变量由 tmp = 10;

The difference is at the "post" step - i.e. after the local variable or parameter has been changed. In the ReturnValue test, this makes no difference to the static value variable. In the OutParameter test, the value variable is changed by the line tmp = 10;

这篇关于返回值或输出参数哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆