Share via


In C# what is default pass by value or reference

Question

Monday, November 7, 2016 8:38 AM

just heard : By default, all parameters are passed by value in C#. Parameters are only passed by reference if you explicitly include an out or ref modifier. However, you need to be aware that when the type of the parameter is a reference type, you're passing a reference rather than an actual object.

is it true ?

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;
    }
}

All replies (3)

Monday, November 7, 2016 8:55 AM

Simple answer: Yes it is true.

All values are passed by value unless specifically stated as a ref or out parameter.

But you need to remember that a reference variable, i.e. a non-value-type variable such as an object, is already a reference. So even if you pass that reference variable into a method that takes a normal 'by value' argument (not a 'by ref' or 'out' argument), its actually a reference that you are passing in.

static void Main()
{

 int i = 10;     // value type variable
 int j = 20;     // value type variable
 SomeObject sobj = new SomeObject(); // reference type variable. i.e. this is a 'pointer' to an object in memory

 MyMethod(i, sobj);

}


public void MyMethod(int x, SomeObject sobj, ref int y)
{
  // x and sobj both passed by value. But sobj is already a reference (a 'pointer') to an object in memory.

  // y is a ref argument. This means it is a reference (a 'pointer') back to the original variable j that was passed in by calling code.
}

Monday, November 7, 2016 9:56 AM | 1 vote

yes. While we are passing by reference we have to be extra careful because we are changing actual value of that reference.  

Pass by value we just send a copy of that field

Best image i  have found

MD. ROKON-UZ-ZAMAN

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Monday, November 7, 2016 3:35 PM

To clarify, from an academic point of view, ref and out are 2 different kinds of parameters. Language design identifies 3 kinds of parameters:

Input (aka pass by value) - Argument passed into function can be read/written by function but changes are not seen outside the function. This is pretty much always implemented by copying the parameter value but there are no requirements, provided the rules are followed. Every language I'm aware of default to input parameters as it makes the most sense and is easiest.

Output - Value is passed back by function and not provided by caller. This value is writable from a function but isn't generally readable. Some languages (like C#) support this but other languages (like C++) do not. The return type is close enough that we generally consider it to be an output parameter but it technically isn't. In C# an output parameter can be read or written but it has an undefined value until the function writes to it first. Some languages enforce the write-only rule.

Input/output (aka pass by reference) - Argument passed into function and can be read/written by function. Changes made in function will impact original argument. In almost all languages this is implemented by having the argument (it needs to be a variable) and parameter point to the same memory location temporarily. Hence when you make a change to the parameter it immediately changes the argument. But this is not required provided the argument is updated upon return. The reference approach just makes things easier.

Michael Taylor
http://www.michaeltaylorp3.net