Microsoft Developer Network >
Página Inicial dos Fóruns
>
Visual C# General
>
Referencing to Objects from Other Object
Referencing to Objects from Other Object
- Hi, I am kind of new using C#, I am a programmer from C++.I know that C# has a garbage collector but I have a question about referencing objects from other objects.I have an Object "Car" and other object "Motor", so that:
public class Car { public Motor TheMotor; ... } public class Motor { public Car TheCarForThisMotor; ... }
When I start my application these objects are created and assigned:Car TheCar = new Car(); TheCar.TheMotor = new Motor(); TheCar.TheMotor.TheCarForThisMotor = TheCar;
Later on in the application I need to update (upgrade) the Motor object of the car, so that, I got a new Motor:void program_event { //got a better Motor: Motor SuperMotor = new Motor(); TheCar.Motor = SuperMotor; //What happen with the "old motor"? SuperMotor.TheCarForThisMotor = TheCar; //What happen with the reference of the "old motor" to the car }
Question:I am overwriting the value of the TheCar.Motor with other object and also the "old motor" was making a reference of the TheCar (TheCarForThisMotor).What happen in this case, the garbage collector would take care of the "old motor"? What happen with the reference to the original car object (does it still exist?)?Should I get firts the old motor and assign null to TheCarForThisMotor?:Motor OldMotor = TheCar.Motor; OldMotor = null; //got a better Motor: Motor SuperMotor = new Motor(); TheCar.Motor = SuperMotor; SuperMotor.TheCarForThisMotor = TheCar
Thanks,kikeman.
Respostas
- No, you don't need to assign null first. It'll be determined that the motor isn't accessible anymore, and the motor will be garbage collected. The GC is smart enough, however, to know not to also collect TheCar, as it's still accessible.
I wrote an article on how the GC works here:
http://wiki.codinglight.com/GarbageCollector.ashx
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marcado como RespostaKikeman quarta-feira, 4 de novembro de 2009 19:43
Todas as Respostas
- No, you don't need to assign null first. It'll be determined that the motor isn't accessible anymore, and the motor will be garbage collected. The GC is smart enough, however, to know not to also collect TheCar, as it's still accessible.
I wrote an article on how the GC works here:
http://wiki.codinglight.com/GarbageCollector.ashx
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marcado como RespostaKikeman quarta-feira, 4 de novembro de 2009 19:43
- Thanks,Another question, which has something to do with the last question.I have a collection of Window Forms (collection of MyForm object, static List<>). These forms are created and shown from MainForm. Each MyForm is constantly shown and it draws a chart according to a member object of MyForm called MyData. It is to say:class MyForm : Form{Public MyDataObject MyData;}In this case the MainForm will run some reading in a database, so that, will be updating the MyData object in order to show it the MyForms objects.If I have all these forms being shown in the screen and maybe the user may resize them and MyForm will read the MyData to re-draw the chart. The main form will be updating the information like this.void MyUpdateDataEvent{MyForms[X].MyData = MyNewData;}Is this thread safe if someone is resizing the MyForm while the MainForm updates the data in the MyForms collection?Thank for your time man,kikeman.
It should be fine, as they're completely unrelated operations, but to be honest, that question is pretty far removed from the first. If you have any more questions, please start a new thread.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser

