4

I am not quiet sure about the concept of "passing by value" and "passing by reference" in c#. I think Passing by value mean :

    int i = 9;

we pass int i into a method like: method(i) Passing by reference means exactly passing its location like :

    Class.method.variable

and it will give out the value. Can anybody help me out??

Jay Singh
  • 85
  • 1
  • 1
  • 5

5 Answers5

10

In simple terms...

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9.

"Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

This has various consequences, and each is useful in different situations.

This answer has more thorough information: What's the difference between passing by reference vs. passing by value?

Community
  • 1
  • 1
Jacob
  • 383
  • 1
  • 5
  • 11
4

Few Points:

In C# unless, you use ref keyword, it is always Pass By Value.

Pass By Value: In case of Value Type - It is just another copy of Value Type Variable.

In case of Reference Type - It is a copy of reference (or pointer) to exact location where memory is assigned in heap.

Pass By Reference:

for Reference type variable - passing it to another method by using keyword is only useful in the scenario when the passed variable can be reassigned in the called method. It means :

let's say - Outside (caller) method A inside (called) method B

Reference Type Object R -passed to method B from method A then if you reassign the memory location of Object R in method B then those changes wouldn't be seen in Method A of Object R unless you pass the object R using ref keyword (i.e pass by reference).

James
  • 173
  • 1
  • 6
  • `In C# unless, you use ref keyword, it is always Pass By Value.` This is not correct. This is correct for simple types like int and bool, but a class is passed by reference by default. – crthompson Oct 21 '13 at 22:57
  • 2
    @paqogomez - Instances of class types are **not** passed by reference by default, `ref` or `out` are required to pass by reference in C#. – Lee Oct 21 '13 at 23:03
  • @paqogomez: NO, it is 100% correct. Reference types work the same as all other types. Without `ref` or `out` it is pass by value. – newacct Oct 24 '13 at 02:45
  • Jon Skeet clears this up quite well: http://stackoverflow.com/a/8708674/543538 and http://jonskeet.uk/csharp/parameters.html – GrandMasterFlush Jan 27 '15 at 20:08
2

This is a very extensive subject so I will only try to answer your direct question. Passing something by value or reference to a method, will depend on what you're actually sending.

If it's a value type it will be sent by value, if it's a reference type it will send the reference by value. Basically it will send the variable itself to the method.

If you want true referencing, you have to use the ref keyword.

An example to showcase it:

void Main()
{
    int x = 5;
    Console.WriteLine (x);
    Test(x);
    Console.WriteLine (x);

    MyClass c = new MyClass();
    c.Name = "Jay";
    Console.WriteLine (c.Name);
    TestClass(c);
    Console.WriteLine (c.Name);
}

private void Test(int a){
 a += 5;
}

private void TestClass(MyClass c){
 c.Name = "Jeroen";
}

public class MyClass {
 public String Name {get; set; }
}

Output:

5
5
Jay
Jeroen

An int is a value type and thus its value is sent to the method. This means that any adjustment on that parameter will only be done on the local copy inside the method.

However, a class is a reference type and thus myClass will send its memory location. This means that by changing the data inside the given object, this will also change the value of this class in different scopes because they both refer to the same memory location.

If you'd use the ref keyword, the reference would be transmitted.

If you want more information, take it from a much more experienced person than me.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

pseudo code example :

       main()
       { 
           //Original value of 'x' will remain unchanged 
           // in case of call-by-value 
   
           int x = 5;

           print( "Value of x before call-by-value: " + x);
           // 5   

           fun(x);

           print("Value of x after call-by-value: " + x);
           // 5 (in case of languages like JAVA which are pass by value)
       }

       void fun(int x) 
       { 
           x=x+10;          
       }

Now coming to the meaning - the value of x did not change in the main function - this is called pass by value.

If the value changes in the main function it is called pass by reference.

refer this for clear understanding between pass by value and pass by reference

What is Pass by Value?

In pass by value, the value of a function parameter is copied to another location of the memory. When accessing or modifying the variable within the function, it accesses only the copy. Thus, there is no effect on the original value.

What is Pass by Reference?

In pass by reference, the memory address is passed to that function. In other words, the function gets access to the actual variable.

Difference Between Pass by Value and Pass by Reference

Definition

Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.

Changes

In pass by value, the changes made inside the function are not reflected in the original value. On the other hand, in pass by reference, the changes made inside the function are reflected in the original value. Hence, this is another difference between pass by value and pass by reference.

Actual Parameter

Moreover, pass by value makes a copy of the actual parameter. However, in pass by reference, the address of the actual parameter passes to the function.

ADITYA AHLAWAT
  • 140
  • 1
  • 13
-1

Passing by value means you are passing the actual reference to the location in memory for that object. So if you have a class, something like:

CustomObj aObj = New CustomObj();
aObj.SomeString = "Test";
someMethod(aObj);

And a method header like so:

someMethod(CustomObj objIn){
     objIn.SomeString = "Changing by value";
}

And wrote out to the console:

Console.WriteLine(aObj.SomeString)

It would produce "Changing by value" instead of "Test" because we have a reference to that object.

By value, conversely, does not work this way.Keeping with our example above:

int byValue = 0;
someMeothd(byValue);

someMethod(int numIn){
   numIn++;
} 

Console.WriteLine(byValue);

Would produce "0".

Jason Renaldo
  • 2,802
  • 3
  • 37
  • 48
  • 1
    Neither of these are pass by reference. In C#, passing by reference requires using the `ref` or `out` modifiers. – Lee Oct 21 '13 at 22:40
  • Ah, I thought he was asking just about the concept of passing reference or value types – Jason Renaldo Oct 21 '13 at 23:17
  • @Lee I'm curious, why does the selected answer address the same thing I do then? – Jason Renaldo Nov 05 '13 at 15:59
  • As it stands, your answer discusses the different between passing a reference type variable (`CustomObj`) by value, and passing a value type variable by value, namely that you can mutate an object through different copies of the same reference. The accepted answer explains that passing by reference creates an alias to the variable in the calling method, so `object o; SomeMethod(out o);` will actually cause `o` itself to be modified, not just the object it is pointing to. – Lee Nov 05 '13 at 17:20