removing string reference question

  • Thread starter Thread starter Goose14
  • Start date Start date
G

Goose14

string SelectedSite
using (SiteLauncher SiteLaunch = new SiteLauncher()
{
SiteLaunch.ShowDialog();
SelectedSite = SiteLaunch.SelectedPokerRoom; //this is
a property that returns a string
}

My question is, if after the using block is over, will the SiteLaunch
object be able to dispose itself? Or does it stay alive because of the
open reference with SelectedSite. If this is the case, is there a
better way of doing this?

Thanks
 
Goose14 said:
string SelectedSite
using (SiteLauncher SiteLaunch = new SiteLauncher()
{
SiteLaunch.ShowDialog();
SelectedSite = SiteLaunch.SelectedPokerRoom; //this is
a property that returns a string
}

My question is, if after the using block is over, will the SiteLaunch
object be able to dispose itself? Or does it stay alive because of the
open reference with SelectedSite. If this is the case, is there a
better way of doing this?

No, it's fine. SiteLaunch may refer to the string, but the string won't
refer to the SiteLaunch.
 
string SelectedSite
using (SiteLauncher SiteLaunch = new SiteLauncher()
{
SiteLaunch.ShowDialog();
SelectedSite = SiteLaunch.SelectedPokerRoom; //this is
a property that returns a string
}

My question is, if after the using block is over, will the SiteLaunch
object be able to dispose itself? Or does it stay alive because of the
open reference with SelectedSite. If this is the case, is there a
better way of doing this?

SelectedSite doesn't refer to the SiteLauncher instance. It refers to the
string that the SiteLauncher instance returned. So I don't see any reason
that SelectedSite would in any way affect the lifetime of SiteLaunch, or
in any way be affected by the disposal of SiteLaunch.

That said, suppose you have actually referenced the SiteLauncher instance
somehow and retained that reference outside the "using" statement block.
For example, instead of referencing something simple like a string that is
immutable and doesn't contain a reference back to anything else, perhaps
you had a reference to something that was modified by SiteLauncher during
disposal, or which kept a reference to the SiteLauncher object.

Then you would in fact have a problem. My understanding is that not only
"will the SiteLaunch object be able to dispose itself", that's exactly
what the "using" statement always does. It always disposes the object,
before exiting the "using" statement. So if the retained object was
modified by disposal of the SiteLauncher instance, or you were somehow
able to get the SiteLauncher instance reference from the retained object,
that would be an issue. The simple answer is to not do that. :)

Pete
 
string SelectedSite
using (SiteLauncher SiteLaunch = new SiteLauncher()
{
SiteLaunch.ShowDialog();
SelectedSite = SiteLaunch.SelectedPokerRoom; //this is
a property that returns a string
}

My question is, if after the using block is over, will the SiteLaunch
object be able to dispose itself? Or does it stay alive because of the
open reference with SelectedSite. If this is the case, is there a
better way of doing this?

Thanks

Thank you to both.
 

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

Similar Threads


Back
Top