Change object data that is in a list

Asked

Viewed 55 times

0

In my application, there is a chained list of elements. I need to perform an operation to change a data of the chosen element. The question is: can I do in this way as I did below searching for the object and changing the data right there or need to access the direct object of the list and change it there? If I make a change to the object outside the list, the object inside will also change?

        Nodo origem = anel.buscar_nodo(msg.id_origem);
        origem.msg_e.Add(msg);
  • 1

    Can or can’t? When you debugged, what happened?

1 answer

1


TL;DR:

If I make a change to the object outside the list, the object inside will also change?

Yes

Explanation

The objects we create are of the Reference Type (read Ref Type vs Value Type), works as a pointer: the variable actually points to the memory address at which the object’s values are located, so when we change the value of the variable, we’re actually changing the value at which the variable is referencing.

So if we do something like Nodo origem2 = origem1; we are not copying the value of origem1 for origem2, we’re saying:

"origem2, now you will point to the same address that the origem1 is pointing to."

Therefore, when changing a property of origem1 (which is actually changing the value at which it points), this will be reflected in the origem2, pointing to the same object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.