简体   繁体   中英

Confused about references in objects

I am really confused about object relationships! I have two classes Person and Address. Here are the details:

 public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private List<Address> _addresses = new List<Address>(); 

        public void AddAddress(Address address)
        {
            _addresses.Add(address);
            address.Person = this; 
        }

        public List<Address> Addresses  
        {
            get { return _addresses; }
            set { _addresses = value; }
        }
    }

  public class Address
    {
        public string Street { get; set; }


        public Person Person
        {
            get; set;
        }
    }

The AddAddress method in the Person class adds the address to the Addresses collection and also sets the Person for the Address object. Now, in the code I do the following:

var person = new Person() { FirstName = "John", LastName = "Doe" };
            person.AddAddress(new Address() { Street = "Blah Blah" });

            person.Addresses[0].Person = null; 

I am thinking that it should set the person object to null since Addresses[0].Person is pointing to the original person object. But it is not setting it to null. What is going on here?

What is going on here?

Simple Explanation:

A variable, in this case 'Person', holds a reference to the object. Under the hood, this is just a number. When you assign it to null, what you are doing is just stopping that variable pointing at the object. The object itself still exists.

-- Edit

A way to understand how this works, is to literally stand near your computer, and point at it. You are now a variable, and your computer is an object. Now stop pointing at the computer (put your arm down). Now you are a variable that has been assigned null . The computer still exists, nothing has changed about it, the only difference is that you now no-longer point at it.

Address.Person stores a reference to an object. When you ask for Address.Person , it will follow the reference path back to the original object. When you set Address.Person , you are replacing the reference with a new reference. In this case, you are setting it to nothing, or an empty reference.

Address.Person is just a reference to a person. Setting it to null will nullify the reference in the Address class, but the person will still exist. The var you create (person) is also just a reference, so you are not nulling out the person variable. The moment you do null out the person reference (person = null;), the person will get garbage collected.

The Address.Person value is explicitly set in your code when you call AddAddress

public void AddAddress(Address address)        
{            
  _addresses.Add(address);
  address.Person = this;         
}

No where in your code do you remove the reference when you remove the value from the list. In fact if you were to call Person.Addresses.Count, you'd see that it thinks there are more Addresses in it than there actually are (because you set the value to null instead of calling Person.Addresses.Remove(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM