newbie dotnet question - Garbage Collector

H

Huey

This is a total newbie question so please bear with me. I need some
clarification to my understanding.

I understand the garbage collector is responsible for destroying objects and
by default we don't know when that is going to happen. If, for example, I
have a Chair object and one of the properties is a Person object. If the
Person object is not null it means a person is occupying this chair. (I can
fx set Chair.Person.Name="Joe Bloggs";)

When I then want to remove the person from the chair, I could set
Chair.Person=null;

But is this a good design? Can I trust the Person object is destroyed when
coding

if(Chair.Person==null)
{
// seat is now vacant
}

or will the workings of the GC give this an unpredictable result.

In other words, should I control whether a person is occupying a seat in
another way?

Thanks.
 
L

Lloyd Dupont

This is a total newbie question so please bear with me. I need some
clarification to my understanding.

I understand the garbage collector is responsible for destroying objects
and by default we don't know when that is going to happen. If, for
example, I have a Chair object and one of the properties is a Person
object. If the Person object is not null it means a person is occupying
this chair. (I can fx set Chair.Person.Name="Joe Bloggs";)

When I then want to remove the person from the chair, I could set
Chair.Person=null;

But is this a good design?
uh?
how else would you like to say there is no one on the chair?
Can I trust the Person object is destroyed when coding

if(Chair.Person==null)
{
// seat is now vacant
}
no, you can't assume that.
it just means (as quire obvious) that the chair doesn't refer any person
anymore.
or will the workings of the GC give this an unpredictable result.
well, for one thing, if someone else is referencing the person it won't be
deleted.
if no one reference the person, it would be deleted when a garbage
collection occurs (which happens automatically at some well choosen moment).
if it bothers you you could explicitely call the garabage collector with
GC.Collect()
However you should know that the only visible effect of calling GC.Collect()
is to Decrease your performance.
That is slow down your program, worsen its performance.

Well truth to tell it also release any unclaimed native / OS handle. Like a
File system file.
But for such thing there is the IDisposable interface and the
using(IDisposable disp) {} statement.
In other words, should I control whether a person is occupying a seat in
another way?
uh?
seems right to me.

who cares about wether the person has been garbage collected or not.
if chair.Person is null that means that chair.Person is null!
isn't that that matters?
 
S

stooferus

who cares about wether the person has been garbage collected or not.
if chair.Person is null that means that chair.Person is null!
isn't that that matters?

The only time you shold really care about this is if Person holds onto
resources such as files and you need to ensure that they get closed.
In this situation you should implement the IDisposable interface, which
allows you to explicitly Dispose of objects which you are done with and
perform any necessary cleanup tasks..
 
H

Huey

who cares about wether the person has been garbage collected or not.
if chair.Person is null that means that chair.Person is null!
isn't that that matters?

What matters is that I need a logic that tells me if a chair is occupied or
not. The question is if Chair.Person == null be used for this purpose? In
other words, would this be a good implementation

public bool SeatOccupied()

{

return (chair.person != null);

}



or should I rather control it using

public bool SeatOccupied()

{

return seatOccupied;

//where seatOccupied is a bool where value is set at the appropiate places.

}
 
S

skeet

Huey said:
What matters is that I need a logic that tells me if a chair is occupied or
not. The question is if Chair.Person == null be used for this purpose?
Absolutely.

In other words, would this be a good implementation

public bool SeatOccupied()
{
return (chair.person != null);
}

Well, I'd make that a property:

public bool SeatOccupied
{
get
{
return chair.Person != null;
}
}

But other than that, it's fine.
or should I rather control it using

public bool SeatOccupied()

{
return seatOccupied;
//where seatOccupied is a bool where value is set at the appropiate places.
}

That's a worse idea IMO - you've effectively got two bits of
information which you'd have to keep in sync. Using null to represent
an empty chair is a perfectly valid design.

Jon
 
T

Truong Hong Thi

In addition, you was worried about GC when you did not need to. I want
to emphasize that when for sure you can check "if (Chair.Person==null)"
without worrying about GC. It has nothing to do with GC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top