I'm working on a small app on .NET, I need to point the same data with 2 diferent lists, I'm wondering if perhaps the memory is duplicated, for example
public class Person
{
    public string name;
    public int age;
    ....
    public Person(name, age)
    {
        this.name = name;
        this.age = age;
    }
}
SortedList<string> names;
SortedList<int> ages;
Person person1 = new Person("juan",23);
names.add("juan",person1);
ages.add(23,person1);
I guess that .NET as Java will not duplicate the object Person, so it will be keeped, so if I do this:
names("juan").age = 24
Will change the object in both lists.
Is it right?
Thank you.
Because Person is a class, you are only putting a reference into the list, so each list will have a reference to the same person. The Person object will indeed not be duplicated, and names["juan"] will de-reference the original object.
However! That doesn't make the code faultless:
SortedList<,> won't like thatnames["juan"] won't automatically update ages; ages[24] will failIf Person was a struct, then the Person would be copied every time you assign it (but: this is not a good candidate for a struct; don't do that)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With