Passing Objects By Reference or Value in C#

C#Parameter PassingPass by-ReferencePass by-Value

C# Problem Overview


In C#, I have always thought that non-primitive variables were passed by reference and primitive values passed by value.

So when passing to a method any non-primitive object, anything done to the object in the method would effect the object being passed. (C# 101 stuff)

However, I have noticed that when I pass a System.Drawing.Image object, that this does not seem to be the case? If I pass a system.drawing.image object to another method, and load an image onto that object, then let that method go out of scope and go back to the calling method, that image is not loaded on the original object?

Why is this?

C# Solutions


Solution 1 - C#

Objects aren't passed at all. By default, the argument is evaluated and its value is passed, by value, as the initial value of the parameter of the method you're calling. Now the important point is that the value is a reference for reference types - a way of getting to an object (or null). Changes to that object will be visible from the caller. However, changing the value of the parameter to refer to a different object will not be visible when you're using pass by value, which is the default for all types.

If you want to use pass-by-reference, you must use out or ref, whether the parameter type is a value type or a reference type. In that case, effectively the variable itself is passed by reference, so the parameter uses the same storage location as the argument - and changes to the parameter itself are seen by the caller.

So:

public void Foo(Image image)
{
    // This change won't be seen by the caller: it's changing the value
    // of the parameter.
    image = Image.FromStream(...);
}

public void Foo(ref Image image)
{
    // This change *will* be seen by the caller: it's changing the value
    // of the parameter, but we're using pass by reference
    image = Image.FromStream(...);
}

public void Foo(Image image)
{
    // This change *will* be seen by the caller: it's changing the data
    // within the object that the parameter value refers to.
    image.RotateFlip(...);
}

I have an article which goes into a lot more detail in this. Basically, "pass by reference" doesn't mean what you think it means.

Solution 2 - C#

Lots of good answers had been added. I still want to contribute, might be it will clarify slightly more.

When you pass an instance as an argument to the method it passes the copy of the instance. Now, if the instance you pass is a value type(resides in the stack) you pass the copy of that value, so if you modify it, it won't be reflected in the caller. If the instance is a reference type you pass the copy of the reference(again resides in the stack) to the object. So you got two references to the same object. Both of them can modify the object. But if within the method body, you instantiate new object your copy of the reference will no longer refer to the original object, it will refer to the new object you just created. So you will end up having 2 references and 2 objects.

Solution 3 - C#

One more code sample to showcase this:

void Main()
{
	
	
	int k = 0;
	TestPlain(k);
	Console.WriteLine("TestPlain:" + k);
	
	TestRef(ref k);
	Console.WriteLine("TestRef:" + k);
	
	string t = "test";
	
	TestObjPlain(t);
	Console.WriteLine("TestObjPlain:" +t);
	
	TestObjRef(ref t);
	Console.WriteLine("TestObjRef:" + t);
}

public static void TestPlain(int i)
{
	i = 5;
}

public static void TestRef(ref int i)
{
	i = 5;
}

public static void TestObjPlain(string s)
{
	s = "TestObjPlain";
}

public static void TestObjRef(ref string s)
{
	s = "TestObjRef";
}

And the output:

> TestPlain:0 > > TestRef:5 > > TestObjPlain:test > > TestObjRef:TestObjRef

Solution 4 - C#

I guess its clearer when you do it like this. I recommend downloading LinqPad to test things like this.

void Main()
{
	var Person = new Person(){FirstName = "Egli", LastName = "Becerra"};
	
	//Will update egli
    WontUpdate(Person);
	Console.WriteLine("WontUpdate");
    Console.WriteLine($"First name: {Person.FirstName}, Last name: {Person.LastName}\n");
	
	UpdateImplicitly(Person);
	Console.WriteLine("UpdateImplicitly");
    Console.WriteLine($"First name: {Person.FirstName}, Last name: {Person.LastName}\n");

    UpdateExplicitly(ref Person);
	Console.WriteLine("UpdateExplicitly");
    Console.WriteLine($"First name: {Person.FirstName}, Last name: {Person.LastName}\n");
}

//Class to test
public class Person{
	public string FirstName {get; set;}
	public string LastName {get; set;}
	
	public string printName(){
		return $"First name: {FirstName} Last name:{LastName}";
	}
}

public static void WontUpdate(Person p)
{
    //New instance does jack...
	var newP = new Person(){FirstName = p.FirstName, LastName = p.LastName};
	newP.FirstName = "Favio";
	newP.LastName = "Becerra";
}

public static void UpdateImplicitly(Person p)
{
    //Passing by reference implicitly
	p.FirstName = "Favio";
	p.LastName = "Becerra";
}

public static void UpdateExplicitly(ref Person p)
{
    //Again passing by reference explicitly (reduntant)
	p.FirstName = "Favio";
	p.LastName = "Becerra";
}

And that should output

WontUpdate

First name: Egli, Last name: Becerra

UpdateImplicitly

First name: Favio, Last name: Becerra

UpdateExplicitly

First name: Favio, Last name: Becerra

Solution 5 - C#

When you pass the the System.Drawing.Image type object to a method you are actually passing a copy of reference to that object.

So if inside that method you are loading a new image you are loading using new/copied reference. You are not making change in original.

YourMethod(System.Drawing.Image image)
{
    //now this image is a new reference
    //if you load a new image 
    image = new Image()..
    //you are not changing the original reference you are just changing the copy of original reference
}

Solution 6 - C#

How did you pass object to method?

Are you doing new inside that method for object? If so, you have to use ref in method.

Following link give you better idea.

http://dotnetstep.blogspot.com/2008/09/passing-reference-type-byval-or-byref.html

Solution 7 - C#

In the latest version of C#, which is C# 9 at this time of writing, objects are by default passed by ref. So any changes made to the object in the calling function will persist in the object in the called function.

Solution 8 - C#

Employee e = new Employee();
e.Name = "Mayur";

//Passes the reference as value. Parameters passed by value(default).
e.ReferenceParameter(e);

Console.WriteLine(e.Name); // It will print "Shiv"

 class Employee {

   public string Name { get; set; }

   public void ReferenceParameter(Employee emp) {

     //Original reference value updated.
    emp.Name = "Shiv";

    // New reference created so emp object at calling method will not be updated for below changes.
    emp = new Employee();
    emp.Name = "Max";
  }
}

Solution 9 - C#

In Pass By Reference You only add "ref" in the function parameters and one more thing you should be declaring function "static" because of main is static(#public void main(String[] args))!

namespace preparation
{
  public  class Program
    {
      public static void swap(ref int lhs,ref int rhs)
      {
          int temp = lhs;
          lhs = rhs;
          rhs = temp;
      }
          static void Main(string[] args)
        {
            int a = 10;
            int b = 80;
          
  Console.WriteLine("a is before sort " + a);
            Console.WriteLine("b is before sort " + b);
            swap(ref a, ref b);
            Console.WriteLine("");
            Console.WriteLine("a is after sort " + a);
            Console.WriteLine("b is after sort " + b);  
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#OlegIView Answer on Stackoverflow
Solution 3 - C#vmgView Answer on Stackoverflow
Solution 4 - C#Egli BecerraView Answer on Stackoverflow
Solution 5 - C#Haris HasanView Answer on Stackoverflow
Solution 6 - C#dotnetstepView Answer on Stackoverflow
Solution 7 - C#SujoyView Answer on Stackoverflow
Solution 8 - C#Mayur PawarView Answer on Stackoverflow
Solution 9 - C#user5593590View Answer on Stackoverflow