Jon Skeet [C# MVP] a écrit :
Wing Siu <wi******@hotmail.comwrote:
>I have a class named Brand
Under the Brand, it may contains several exhibition, and the relationship of
Brand:Exhibition is 1:M
Then I am using this design
class Brand {
ArrayList Exhibitions;
}
But in Exhibition, do I need indicate it's Brand?
For example:
class Exhibition {
Brand brand;
}
That depends - do you need to be able to navigate from a Brand to an
Exhibition in your code? If you don't, it would be gneerally nicer not
to have the bidirectional link - but if it's something you need, you
can certainly do it with no problems.
I have a question about this : is the Garbage Collector able to detect
(and handle) circular references ? If not (which I assume), the code
above will lead to memory "leaks".
If you create and reference a Brand in your application. Assume that
this brand has 1 Exhibition : the GC sees 1 reference to your Exhibition
(from Brand), and 2 references to your Brand (1 from the application, 1
from the Exhibition). If you free your reference to Brand in the
application, you will expect the GC to elect Brand and Exhibition, but
unfortunately there still exists one reference for each, so that they
will not be collected until the end of your application.
The solution is to use a WeakReference (see MSDN) in your Exhibition to
reference the Brand.
Mathieu