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

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

问题描述

如果我们想从一个方法的价值,我们可以使用返回值,如:

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?

感谢您。

推荐答案

返回值都差不多的总是的时候方法没有什么要回到正确的选择。 (事实上​​,我不能想到这里我的曾经的希望与退出参数无效方法的任何情况下,如果我有选择)。

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

除了别的,它无需单独声明变量停止来电:

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

int foo;
GetValue(out foo);

VS

int foo = GetValue();

淘汰的价值观上也prevent方法链是这样的:

Out values also prevent method chaining like this:

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

(事实上,这是与属性setter的问题之一,也是,这就是为什么制造商模式使用它返回的建设者,如 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

所不同的是在后一步 - 即局部变量或参数已经被改变了。在返回值的测试,这使得静态变量没有什么区别。在OutParameter测试中,变量是由行变 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;

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

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