Passing variable by Ref. or Value Dilemma

蓝咒 提交于 2020-01-06 11:49:33

问题


I have some void methods which are static.

Is it better to pass variables by reference or by value, because I'm passing large amount of text into these variables :

public static void renderText(ref StringBuilder build)
{
  //Do your job.
}

So could someone explain me, what happens when I send a reference of StringBuilder, does it only access this StringBuilder? (It does not copy it right!).

And just in case I'm not changing the value or any other property of input arguments into methods.

So, in this cases where variables are huge enough and not manipulated, should I always send the reference of it, and if yes does it interfere with something?


回答1:


Take a look at the following article by Jon Skeet in which he thoroughly explains the difference between passing by reference or by value.

Parameter passing in C#

or this blog post which illustrates the former article:

http://rapidapplicationdevelopment.blogspot.com/2007/01/parameter-passing-in-c.html

The last article actually uses the StringBuilder type in its examples, so you can clearly see what's going on in your case. If you pass in the StringBuilder type by reference you'll get this:

Reference type passed by reference:




回答2:


As long as you're dealing with reference types (classes, which StringBuilder and String are), there's rarely a point in passing them by reference since no copy will be made anyways.




回答3:


Non-value types are always passed by reference. The purpose of ref keyword is to let you change the reference to the object inside the method.

You misunderstood the arguments passing completely. Read this article.




回答4:


Passing arguments by value

By default, arguments in C# are passed by value. This means a copy of the value is created when passed to the method:

class Test
{
    static void Foo(int p)
    {
        p = p + 1;             // Increment p by one.
        Console.WriteLine(p);  // Write p to screen.
    } 

    static void Main()
    {
        int x = 8;            
        Foo(x);               // Make a copy of x.
        Console.WriteLine(x); // x will still be 8.
    }
}

Assigning p a new value does not change the contents of variable x, because p and x reside in different memory locations.

Passing a reference-tupe argument by value copies the reference, but not the object. An example in the case of string builder; here Foo sees the same StringBuilder object that main instantiated, but has an independent reference to it. So StrBuild and fooStrBuild are seperate variables that reference the same StringBuilder object:

class Test
{
    static void Foo(StringBuilder fooStrBuild)
    {
        fooStrBuild.Append("testing");            
        fooStrBuild = null;
    } 

    static void Main()
    {
        StringBuilder StrBuild = new StringBuilder();
        Foo(strBuild);
        Console.WriteLine(StrBuild.ToString()); // "testing"
    }
}

Because fooStrBuild is a copy of a reference changing its value does not change StrBuild.

Pass by reference

In the following, p and x refer to the same memory locations:

class Test
{
    static void Foo(ref int p)
    {
        p = p + 1;             // Increment p by one.
        Console.WriteLine(p);  // Write p to screen.
    } 

    static void Main()
    {
        int x = 8;            
        Foo(ref x);           // Make a copy of x.
        Console.WriteLine(x); // x is now 9.
    }
}

hence the value of p is changed.

Hope this helps.



来源:https://stackoverflow.com/questions/7398266/passing-variable-by-ref-or-value-dilemma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!