Newbie Problems
#10


 

I wouldn't call reader.close() in a using statement simply because that's not necessary.

The using directive calls Dispose() after executing and that takes care of closing the reader.

 

Side note:

 

If you want to implement it in your own classes e.g if your objects use unmanaged resources such as file handles:

That means every object x in the using statement [using (foo x = new foo())] has to implement the IDisposable interface inorder to use the using statement. 

You can use multiple using statements at once.

You can't access the object outside the using statement.

Usualy the Dispose method is implemented as that:

 

public class Foo : IDisposable
{
     private bool disposed = false;

/*
Implementation of your class
onmitted
*/
 
public void Dispose() {
Dispose(true);
GC.SupressFinialize(this);
}
 
protected virtual void Dispose(bool disposeManaged) {
        
        if(disposed){
            return;
        }

if(disposeManaged){
//take care of managed objects here
}
//take care of unmanaged object here

        disposed = true;
}
}

 

why should he care about unmanaged objects?

I dont understand what u trying to explain Tongue

If its about memory then its useless because there is a GC that destroys the not used objects.(Only if there is no reference to it)



Messages In This Thread
[No subject] - by dethunter12 - 03-27-2015, 03:30 AM
[No subject] - by Nikolee - 03-27-2015, 07:40 AM
[No subject] - by Wizatek - 03-27-2015, 10:19 AM
[No subject] - by dethunter12 - 03-27-2015, 11:57 AM
[No subject] - by Nikolee - 03-27-2015, 01:04 PM
[No subject] - by Tarissuis - 03-27-2015, 05:40 PM
[No subject] - by Wizatek - 03-27-2015, 05:54 PM
[No subject] - by Wahrheit - 03-28-2015, 05:26 PM
[No subject] - by DamonA - 03-28-2015, 05:44 PM
[No subject] - by Nikolee - 03-28-2015, 09:30 PM
[No subject] - by Wahrheit - 03-28-2015, 10:22 PM
[No subject] - by Nikolee - 03-29-2015, 09:03 AM
[No subject] - by CDWriter - 04-14-2015, 02:36 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)