473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Whats the best way to kill an object?

I'm writing a WPF app in c# and want to know the best way to get rid of an
object. this is how its created

someclass obj = new someclass();
void CreatObjects()
{
aCollection.Add(obj);
AnotherClass obj2=new AnotherClass(obj);
aNotherCollection.Add(obj2);
)

Now I want to completely get rid of this instance of obj and obj2

void KillObjects(obj, obj2)
{
aCollection.Remove(obj);
aNotherCollection.Remove(obj2);
//Since at this moment obj and obj2 are alive in this method, should I set
the to null like this?
obj=null;
obj2=null;
}

What is the most appropriate what to get rid of obj and obj2?

Thanks.


--
mo*******@noemail.noemail
Feb 28 '07 #1
2 1044
moondaddy <mo*******@noemail.noemailwrote:
I'm writing a WPF app in c# and want to know the best way to get rid of an
object. this is how its created

someclass obj = new someclass();
void CreatObjects()
{
aCollection.Add(obj);
AnotherClass obj2=new AnotherClass(obj);
aNotherCollection.Add(obj2);
)

Now I want to completely get rid of this instance of obj and obj2

void KillObjects(obj, obj2)
{
aCollection.Remove(obj);
aNotherCollection.Remove(obj2);
//Since at this moment obj and obj2 are alive in this method, should I set
the to null like this?
obj=null;
obj2=null;
}

What is the most appropriate what to get rid of obj and obj2?
With the code above, you don't need the last two lines of code - after
all, the method's about to end anyway. It doesn't guarantee that
nothing else will have references to the two objects, however, and even
if nothing does, there's no guarantee that the objects will be garbage
collected any time soon.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #2
OK thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
moondaddy <mo*******@noemail.noemailwrote:
>I'm writing a WPF app in c# and want to know the best way to get rid of
an
object. this is how its created

someclass obj = new someclass();
void CreatObjects()
{
aCollection.Add(obj);
AnotherClass obj2=new AnotherClass(obj);
aNotherCollection.Add(obj2);
)

Now I want to completely get rid of this instance of obj and obj2

void KillObjects(obj, obj2)
{
aCollection.Remove(obj);
aNotherCollection.Remove(obj2);
//Since at this moment obj and obj2 are alive in this method, should I
set
the to null like this?
obj=null;
obj2=null;
}

What is the most appropriate what to get rid of obj and obj2?

With the code above, you don't need the last two lines of code - after
all, the method's about to end anyway. It doesn't guarantee that
nothing else will have references to the two objects, however, and even
if nothing does, there's no guarantee that the objects will be garbage
collected any time soon.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
1
by: kmunderwood | last post by:
I have an xml file that I get "As Is" (at bottom of post) I want to sort and exclude some elements, and turn other child elements red, or its background. I want it to look like this: Tank ...
176
by: basecamp | last post by:
just checking the average age of programmers using this group -- thanks
3
by: Guy Bradley | last post by:
write one function (for navigation) and call it in multiple asp.net pages? - i would use an include file as i do in asp but i gather this isnt the best way of doing it... suggestion? - thx in...
2
by: charmis | last post by:
Hi everyone, I am trying to include one feature using excel object in my application. I am giving the code to create and kill the process , that i got from Microsoft site.......but nothing is...
4
by: dgleeson3 | last post by:
Hello All I have three classes Form1, DataBase_Manager and Users. Form 1 creates two object of type DataBase_Manager and Users as below. Public Class Form1 Inherits Form Public...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
4
by: bukzor | last post by:
Does anyone have a pythonic way to check if a process is dead, given the pid? This is the function I'm using is quite OS dependent. A good candidate might be "try: kill(pid)", since it throws an...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, Is it possible "kill" the thread of Backgroundworker ? In my Dowork event, I have NOT While for do e.Cancel = true, only have a call to external COM. If I want cancel, calling...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.