Reputation: 615
I have a function which goes like this
public class Component
{
public List<SubComponent1> subComponent1 {get;set;}
public List<SubComponent2> subComponent2 {get;set;}
}
public void Update()
{
List<Component> collection = new List<Component> collection();
//populate from service
collection = getData();
//I want to update subComponent1
List<SubComponent1> copycomp = null;
copycomp = collection.subComponent1;
UpdateService1Data(ref copycomp);
}
public void UpdateService1Data(ref List<SubComponent1> subcopy)
{
//Do some operations and update SubComponent1
//Changes will reflect in collection object
}
My question goes like this:
In the Update()
function copycomp
is referring to the object within collection
variable, so any changes made to the copycomp
variable doesn't need to assign it back to the collection like this :
service.subComponent1 = copycomp ;
since it is a class type and referring to the same object.
But I am passing the same reference to UpdateService1Data()
, then why do I need to make use of ref keyword? Why can't I just pass the variable? If updating works in Update function then why can't it work in UpdateService1Data() . I am visualizing it as the reference or the address to the SubComponent1 class is passed everywhere copycomp is used?
Upvotes: 1
Views: 997
Reputation: 13561
I would suggest reading the accepted answer for What's the difference between passing by reference vs. passing by value?. But the short version is that out and ref are required when (and only when) you want to make it possible to change the variable/field that is used as an argument to the method.
You might think of it as a closure that can’t outlast the calling method.
In your case, if you want UpdateService1Data to be able to mutate the list collection.subComponent1 then it doesn’t need to be ref or out. If you want to be able to make the variable copycomp refer to an entirely different list (or null) then it needs to have a ref or out parameter.
Again, passing by reference means “able to change a variable in the calling method”, if you don’t want to do that you shouldn’t use it.
Upvotes: 0
Reputation: 53958
In the Update() function copycomp is referring to the object within collection variable, so any changes made to the copycomp variable doesn't need to assign it back to the collection like this :
The value of copycomp
is a reference to the object collection.subComponent1
, which is a List<SubComponent1>
. When you call a method and pass copycomp
as an argument (without the use of ref
) you actually pass a copy of this reference, this is the default behavior in C# and in many other high level programming languages. Hence you still have a reference to the original object and you can alter any value there. Specifically in your case you can remove items from the list, add other items, change the value of a property of an item etc.
If the above is that you want you don't need the ref
keyword. You should use the ref
keyword if you want to change the reference that copycomp
holds and then use the new reference for another purpose. In this case you could do any of the actions mentioned previously, which would result in the mutation of the object state that copycomp
points to and in addition to this you could change even the actual reference that copycomp
holds.
Upvotes: 1