简体   繁体   English

使用C#传递值和通过引用传递之间有什么不同

[英]What is different between Passing by value and Passing by reference using C#

I have difficulty understanding the difference between passing by value and passing by reference. 我很难理解传递值和传递参考之间的区别。 Can someone provide a C# example illustrating the difference? 有人可以提供说明差异的C#示例吗?

In general, read my article about parameter passing . 一般来说,阅读我关于参数传递的文章

The basic idea is: 基本思路是:

If the argument is passed by reference, then changes to the parameter value within the method will affect the argument as well. 如果参数是通过引用传递的,那么对方法中参数值的更改也会影响参数。

The subtle part is that if the parameter is a reference type, then doing: 细微的部分是,如果参数是引用类型,那么执行:

someParameter.SomeProperty = "New Value";

isn't changing the value of the parameter. 不会更改参数的值。 The parameter is just a reference, and the above doesn't change what the parameter refers to, just the data within the object. 该参数只是一个引用,上面的内容不会改变参数引用的内容,只会更改对象中的数据。 Here's an example of genuinely changing the parameter's value: 以下是真正更改参数值的示例:

someParameter = new ParameterType();

Now for examples: 现在举个例子:

Simple example: passing an int by ref or by value 简单示例:通过ref或value传递int

class Test
{
    static void Main()
    {
        int i = 10;
        PassByRef(ref i);
        // Now i is 20
        PassByValue(i);
        // i is *still* 20
    }

    static void PassByRef(ref int x)
    {
        x = 20;
    }

    static void PassByValue(int x)
    {
        x = 50;
    }
}

More complicated example: using reference types 更复杂的例子:使用引用类型

class Test
{
    static void Main()
    {
        StringBuilder builder = new StringBuilder();
        PassByRef(ref builder);
        // builder now refers to the StringBuilder
        // constructed in PassByRef

        PassByValueChangeContents(builder);
        // builder still refers to the same StringBuilder
        // but then contents has changed

        PassByValueChangeParameter(builder);
        // builder still refers to the same StringBuilder,
        // not the new one created in PassByValueChangeParameter
    }

    static void PassByRef(ref StringBuilder x)
    {
        x = new StringBuilder("Created in PassByRef");
    }

    static void PassByValueChangeContents(StringBuilder x)
    {
        x.Append(" ... and changed in PassByValueChangeContents");
    }

    static void PassByValueChangeParameter(StringBuilder x)
    {
        // This new object won't be "seen" by the caller
        x = new StringBuilder("Created in PassByValueChangeParameter");
    }
}

Passing by value means a copy of an argument is passed. 按值传递意味着传递参数的副本。 Changes to that copy do not change the original. 对该副本的更改不会更改原始副本。

Passing by reference means a reference to the original is passed and changes to the reference affect the original. 通过引用传递意味着传递对原始的引用,并且对引用的更改会影响原始引用。

This is not specific to C#, it exists in many languages. 这不是C#特有的,它以多种语言存在。

The digest is: 摘要是:

Passing by reference is used when you expect the function/method to modify your variable. 当您希望函数/方法修改变量时,将使用按引用传递。

Passing by value when you don't. 当你不这样做时,通过价值传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM