前几天有人问我他们什么时候应该使用参数关键字out而不是ref。虽然我(我认为)理解ref和out关键字之间的区别(之前已经问过了),最好的解释似乎是ref == in和out,有什么(假设或代码)的例子,我应该总是使用out而不是ref。

既然ref更通用,为什么还要用out呢?它只是语法上的糖吗?


当前回答

使用out表示该参数未被使用,仅被设置。这有助于调用者理解您总是在初始化参数。

此外,ref和out不仅适用于值类型。它们还允许您重置引用类型从方法中引用的对象。

其他回答

可以在两个上下文中使用out上下文关键字(每个上下文都是到详细信息的链接),作为参数修饰符或在接口和委托中的泛型类型参数声明中使用。本主题讨论参数修饰符,但有关泛型类型参数声明的信息,请参阅另一个主题。

out关键字使参数通过引用传递。这类似于ref关键字,只不过ref要求在传递变量之前对其进行初始化。要使用out形参,方法定义和调用方法都必须显式使用out关键字。例如: c#

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

虽然作为输出参数传递的变量在传递之前不必初始化,但被调用的方法需要在方法返回之前赋值。

尽管ref和out关键字会导致不同的运行时行为,但它们在编译时不被视为方法签名的一部分。因此,如果唯一的区别是一个方法使用ref参数,而另一个方法使用out参数,则方法不能重载。例如,下面的代码将无法编译: c#

class CS0663_Example
{
    // Compiler error CS0663: "Cannot define overloaded 
    // methods that differ only on ref and out".
    public void SampleMethod(out int i) { }
    public void SampleMethod(ref int i) { }
}

然而,如果一个方法使用ref或out参数,而另一个方法两者都不使用,则可以进行重载,如下所示: c#

class OutOverloadExample
{
    public void SampleMethod(int i) { }
    public void SampleMethod(out int i) { i = 5; }
}

属性不是变量,因此不能作为参数传递。

有关传递数组的信息,请参见使用ref和out传递数组(c#编程指南)。

以下类型的方法不能使用ref和out关键字:

Async methods, which you define by using the async modifier.

Iterator methods, which include a yield return or yield break statement.

例子

当您希望一个方法返回多个值时,声明一个out方法非常有用。下面的示例使用out通过一个方法调用返回三个变量。注意,第三个参数被赋值为null。这使得方法可以有选择地返回值。 c#

class OutReturnExample
{
    static void Method(out int i, out string s1, out string s2)
    {
        i = 44;
        s1 = "I've been returned";
        s2 = null;
    }
    static void Main()
    {
        int value;
        string str1, str2;
        Method(out value, out str1, out str2);
        // value is now 44
        // str1 is now "I've been returned"
        // str2 is (still) null;
    }
}

下面是我从这篇关于c# Out Vs Ref的codeproject文章中摘录的一些注释

只有当我们期望从一个函数或方法得到多个输出时,才应该使用它。对结构的思考也是一个不错的选择。 REF和OUT是关键字,它们指示数据如何从调用方传递到被调用方,反之亦然。 在REF中,数据以两种方式传递。从呼叫者到被呼叫者,反之亦然。 In Out数据仅以一种方式从被调用方传递给调用方。在这种情况下,如果Caller试图向被调用方发送数据,它将被忽略/拒绝。

如果你是一个视觉人,那么请看看这个yourtube视频,它演示了实际的区别https://www.youtube.com/watch?v=lYdcY5zulXA

下图更直观地显示了差异

还是觉得需要好好总结一下,这就是我想出来的。

总结,

当我们在函数内部时,这就是我们如何指定变量数据访问控制,

in = R

out = W必须在R之前

参考 = R+W


解释,

in

函数只能读取该变量。

out

变量不能先初始化,因为, 函数必须先写后读。

ref

函数可以读/写该变量。


为什么要这样命名呢?

关注数据被修改的地方,

in

数据必须在输入(in)函数之前设置。

out

数据必须只在离开(out)函数之前设置。

ref

在输入函数之前必须设置数据。 在离开(out)函数之前可以设置数据。

基本上ref和out都用于在方法之间传递对象/值

out关键字使参数通过引用传递。这类似于ref关键字,只不过ref要求在传递变量之前对其进行初始化。

out:参数没有初始化,必须在方法中初始化

ref:参数已经初始化,可以在方法中读取和更新。

引用类型的“ref”有什么用?

您可以将给定的引用更改为不同的实例。

你知道吗?

Although the ref and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref argument and the other takes an out argument. You can't use the ref and out keywords for the following kinds of methods: Async methods, which you define by using the async modifier. Iterator methods, which include a yield return or yield break statement. Properties are not variables and therefore cannot be passed as out parameters.

读音: Out =只初始化/填充一个参数(参数必须为空),将其plain返回 Ref = reference,标准参数(可能带有value),但函数可以修改它。