Passing Objects By Reference or Value in C#
Solution 1
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
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
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
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
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
}
Related videos on Youtube
Michael
C#, SharePoint, Angular/React software developer. Director at HotSnail
Updated on September 29, 2021Comments
-
Michael over 2 years
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?
-
Andrew Barber over 12 yearsAll variables are passed by value by default in C#. You are passing the value of the reference in the case of reference types.
-
nawfal over 7 years
-
StayOnTarget about 3 yearsSince there was no code given, it is not really clear what is being asked. Maybe the OP meant
image.Load(filename)
or maybe they meantimage = Image.Load(filename)
whereimage
is the function param.
-
-
Michael over 12 yearsYour right, I didnt see that! I loading image = Image.FromFile(..) and that was replacing the variable image and not changing the object! :) of course.
-
Adeem about 9 yearsin simple words can i say if we change the properties or call some function of the parameter object it will be affected but if we initiate the parameter variable then it will be a ref to new location/object. paramX.Caption = "asdasdasd"; //will work paramX = new Object(); //will disconnect it from the caller and that paramX will be ref to new location
-
Jon Skeet about 9 years@Adeem: Not quite - there's no "parameter object", there's the object that the value of the parameter refers to. I think you've got the right idea, but terminology matters :)
-
broadband almost 9 yearsIf we drop keywords
ref
andout
from c#, is it ok to say that c# passes parameters the same way as java does i.e. always by value. Is there any difference with java. -
Jon Skeet almost 9 years@broadband: Yes, the default passing mode is by-value. Although of course C# has pointers and custom value types, which makes it all a bit more complicated than in Java.
-
Jax over 7 yearsSo calling
myobj.Value = null
will be seen by the caller because I changing its internal data. -
Jon Skeet over 7 years@DJMethaneMan: Yes, exactly. That's not changing the parameter (assuming
myobj
is of some reference type...) -
Vippy over 7 yearsSo does this mean that the object being passed without ref or out (default) is "essentially" a copy of that object?
-
Jon Skeet over 7 years@Vippy: No, not at all. It's a copy of the reference. I suggest you read the linked article.
-
Unbreakable almost 6 yearsSo basically reference type still NEED TO BE PASSED as reference if we want to see the changes in the Caller function.
-
Sujoy almost 5 yearsYou forgot to use
out
keyword in the last function. -
Jon Skeet almost 5 years@Sujoy: No I didn't - as per the comment, "it's changing the data within the object that the parameter value refers to". It doesn't change the value of the parameter itself, so it would be pointless for it to be an
out
parameter. -
Marin Popov almost 5 yearsand what about public static void WhatAbout(Person p) { p = new Person(){FirstName = "First", LastName = "Last"}; } . :)
-
JAN over 4 yearsThis should be the chosen answer!
-
JOSEFtw over 4 yearsI agree completely! :)
-
Himalaya Garg about 4 yearsStrings are immutable reference types. Immutable means, it cannot be changed after it has been created. Every change to a string will create a new string. That's why strings needed to be passed as 'ref' to get change in calling method. Other objects (e.g. employee) can be passed without 'ref' to get the changes back in calling method.
-
Daniel about 4 years@vmg, as per HimalayaGarg, this isn't a very good example. You need to include another reference type example which isn't immutable.
-
CAD bloke almost 4 yearsaaaah, examples 1 & 3 pass a copy of the pointer (address) to the source reference type so initially both the source and local things are looking in the same place at the same object but example one changes the pointer of the local copy inside the method so now that is looking at something else, the original pointer is untouched because it can't be touched. Example 2 (ref) uses the same pointer for both so if you mess with it, it changes both.
-
James Ashwood over 3 yearsThanks for the lingpad thing
-
Egli Becerra over 3 yearsLinux4Life531 try this instead of linqpad its free too...dotnetfiddle.net same thing and you dont need to download
-
martinp999 almost 3 yearsWow! I had not thought of this take on byref or byval. By default, dotnet does not quite use either for objects, it uses a copy of the ref , or a new pointer to the old pointer. If a new instance is created, the pointer to that instance must be placed in the memory address created for the input parameter. Many thanks for this insight @JonSkeet
-
Jon Skeet almost 3 years@martinp999: "copy of the ref" is precisely pass by value; a copy of the existing value (which is a reference) is passed, just like all normal pass by value semantics. I don't know what you mean by "the pointer to that instance must be placed in the memory address created for the input parameter" - what do you mean by "must" here? (I'd tend to avoid using the term "pointer" where possible when talking about C#, unless you're actually referring to a C# pointer.)
-
XRaycat over 2 yearsthis does not seem to be the case for me...
-
ProgrammingLlama over 2 yearsWhat is your source for this? Documentation published this month doesn't mention that. Nor does the documentation for passing reference types.