7

I have difficulty understanding the difference between passing by value and passing by reference. Can someone provide a C# example illustrating the difference?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    Duplicate http://stackoverflow.com/questions/373419/whats-the-difference-between-a-parameter-passed-by-reference-vs-passed-by-value –  Aug 18 '09 at 10:47
  • 2
    That's *sort of* a duplicate, but this specifically asks for C# examples - whereas there are no C# examples at all in the referenced question. A lot of good *general* discussion, certainly, but I don't think it really hurts to have language-specific ones too. – Jon Skeet Aug 18 '09 at 11:03
  • @Jon Skeet: Isn't the right solution to stick an answer with C# syntax into the existing question? Presumably editing an existing answer. – dmckee --- ex-moderator kitten Aug 18 '09 at 22:32

3 Answers3

17

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

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");
    }
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

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.

ramosg
  • 2,046
  • 2
  • 15
  • 14