Click here to Skip to main content
15,913,487 members
Home / Discussions / C#
   

C#

 
GeneralRe: Proper object disposal... Pin
Luc Pattyn11-Feb-07 12:45
sitebuilderLuc Pattyn11-Feb-07 12:45 
GeneralRe: Proper object disposal... Pin
Guffa11-Feb-07 12:35
Guffa11-Feb-07 12:35 
GeneralRe: Proper object disposal... Pin
Christian Graus11-Feb-07 12:45
protectorChristian Graus11-Feb-07 12:45 
GeneralRe: Proper object disposal... Pin
Guffa11-Feb-07 14:09
Guffa11-Feb-07 14:09 
GeneralRe: Proper object disposal... Pin
Christian Graus11-Feb-07 14:36
protectorChristian Graus11-Feb-07 14:36 
AnswerRe: Proper object disposal... Pin
Guffa11-Feb-07 12:25
Guffa11-Feb-07 12:25 
GeneralRe: Proper object disposal... Pin
Shy Agam11-Feb-07 23:04
Shy Agam11-Feb-07 23:04 
GeneralRe: Proper object disposal... Pin
Guffa12-Feb-07 4:39
Guffa12-Feb-07 4:39 
shyagam wrote:
If no finalizer is present, where than should I remove the reference to my object?


If class A contains an object of class B that implements IDisposable, class A should also implement IDisposable, so it has a Dispose method where it can call Dispose of the object of class B.

If class A only contains objects that does not implement IDisposable, you don't need to remove the references to the objects.

Example:

Class A {
   private List<string> _list;
   public A() {
      _list = new List<string>();
   }
   public List<string> List { get { return _list; } }
   public Add(string s) {
      _list.Add(s);
   }
}

This is a class that contains a list of strings. As neither the List<> class nor the string class implements IDisposable, class A doesn't have to either. As class A only contains managed objects, you can just leave any objects of class A to the garbage collector, and it will efficiently collect the object, the list that it contains, and every string in the list.


Also, is the last statement in the following code redundent?
I think of it as a good programming practice, as you can be sure that the reference was removed.

public void F()
{
Object obj = new Object();
// Do some stuff with the object
obj = null;
}


You can safely remove the last line.

All local variables are allocated in the stack frame that is created when the method is called, and when the method returns, the stack frame goes away. The variables that were in the stack frame does not even exist any more, so setting the variable to null right before the method ends serves no purpose at all.

---
single minded; short sighted; long gone;

QuestionOpening other programs with C# Pin
Wolf9211-Feb-07 7:33
Wolf9211-Feb-07 7:33 
AnswerRe: Opening other programs with C# Pin
Stefan Troschuetz11-Feb-07 7:48
Stefan Troschuetz11-Feb-07 7:48 
QuestionConecting to database-C# Pin
mrkeivan11-Feb-07 7:23
mrkeivan11-Feb-07 7:23 
AnswerRe: Conecting to database-C# Pin
Mike Hankey11-Feb-07 7:57
mveMike Hankey11-Feb-07 7:57 
GeneralRe: Conecting to database-C# Pin
mrkeivan11-Feb-07 8:43
mrkeivan11-Feb-07 8:43 
GeneralRe: Conecting to database-C# Pin
Mike Hankey11-Feb-07 9:03
mveMike Hankey11-Feb-07 9:03 
AnswerRe: Conecting to database-C# Pin
Guffa11-Feb-07 8:55
Guffa11-Feb-07 8:55 
GeneralRe: Conecting to database-C# Pin
mrkeivan11-Feb-07 10:18
mrkeivan11-Feb-07 10:18 
AnswerRe: Conecting to database-C# Pin
Guffa11-Feb-07 11:20
Guffa11-Feb-07 11:20 
Questionretrieving data from a DataTable by giving the location Pin
Saira Tanwir11-Feb-07 6:44
Saira Tanwir11-Feb-07 6:44 
AnswerRe: retrieving data from a DataTable by giving the location [modified] Pin
Colin Angus Mackay11-Feb-07 7:05
Colin Angus Mackay11-Feb-07 7:05 
QuestionUndo() Method? Pin
Wolf9211-Feb-07 5:43
Wolf9211-Feb-07 5:43 
AnswerRe: Undo() Method? Pin
sharpiesharpie11-Feb-07 6:52
sharpiesharpie11-Feb-07 6:52 
GeneralRe: Undo() Method? Pin
Stefan Troschuetz11-Feb-07 7:55
Stefan Troschuetz11-Feb-07 7:55 
GeneralRe: Undo() Method? Pin
sharpiesharpie11-Feb-07 9:35
sharpiesharpie11-Feb-07 9:35 
GeneralRe: Undo() Method? Pin
Stefan Troschuetz11-Feb-07 20:39
Stefan Troschuetz11-Feb-07 20:39 
AnswerRe: Undo() Method? Pin
Stefan Troschuetz11-Feb-07 7:50
Stefan Troschuetz11-Feb-07 7:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.