Click here to Skip to main content
15,889,216 members
Home / Discussions / C#
   

C#

 
AnswerRe: Read from udp in C# through event Pin
Rick Shaub6-Jan-10 10:25
Rick Shaub6-Jan-10 10:25 
QuestionSend bytes over USB Pin
Frygreen5-Jan-10 23:55
Frygreen5-Jan-10 23:55 
AnswerRe: Send bytes over USB Pin
Dimitri Witkowski6-Jan-10 7:20
Dimitri Witkowski6-Jan-10 7:20 
QuestionPress button on inet-page programmatically [modified] Pin
Frygreen5-Jan-10 23:46
Frygreen5-Jan-10 23:46 
AnswerRe: Press button on inet-page programmatically Pin
Dimitri Witkowski6-Jan-10 7:15
Dimitri Witkowski6-Jan-10 7:15 
AnswerRe: Press button on inet-page programmatically Pin
Frygreen6-Jan-10 9:31
Frygreen6-Jan-10 9:31 
AnswerRe: Press button on inet-page programmatically Pin
Frygreen6-Jan-10 12:41
Frygreen6-Jan-10 12:41 
QuestionValid Reason for Using IDisposable intreface Pin
Lijo Rajan5-Jan-10 22:58
Lijo Rajan5-Jan-10 22:58 
Hi All,
Can anyone please clarify my dout about IDisposable intreface in C#.
We all know tat IDisposable interface is using for disposing unmanaged resources.
I have a class which contains following code. here i have implimented the Dispose method from IDisposable interface.

class ClassA:IDisposable
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");

}
private bool disposed = false;
Image img = null;



public Image Image
{

get { return img; }
}


~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);

}

public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);

// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}


}

What my doubt is why i need to impliment the Dispose method from IDisposable interface?. Instead of that i can create my own Dispose method in my class without inheriting from IDisposable interface which i have given below.

for the class below i haven't inherited my class from IDisposable interface. instead of that i created my own dispose method. this also works fine.
class ClassA
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");

}
private bool disposed = false;
Image img = null;



public Image Image
{

get { return img; }
}


~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);

}

public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);

// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}


}

So can any one tel me tat the reason behind of implimenting dispose method from IDisposable interface. I think everybody understands my question. i searched in google a lot. but i did not get a proper answer.So please clarify my doubt.

Regards
Lijo
AnswerRe: Valid Reason for Using IDisposable intreface Pin
dojohansen5-Jan-10 23:27
dojohansen5-Jan-10 23:27 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Lijo Rajan5-Jan-10 23:38
Lijo Rajan5-Jan-10 23:38 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Saksida Bojan6-Jan-10 0:25
Saksida Bojan6-Jan-10 0:25 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Lijo Rajan6-Jan-10 0:36
Lijo Rajan6-Jan-10 0:36 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Saksida Bojan6-Jan-10 0:49
Saksida Bojan6-Jan-10 0:49 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
dojohansen7-Jan-10 1:38
dojohansen7-Jan-10 1:38 
AnswerRe: Valid Reason for Using IDisposable intreface Pin
Pete O'Hanlon6-Jan-10 2:54
mvePete O'Hanlon6-Jan-10 2:54 
AnswerRe: Valid Reason for Using IDisposable intreface Pin
Gideon Engelberth6-Jan-10 6:33
Gideon Engelberth6-Jan-10 6:33 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Martin#6-Jan-10 21:29
Martin#6-Jan-10 21:29 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
dojohansen7-Jan-10 1:42
dojohansen7-Jan-10 1:42 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Martin#7-Jan-10 2:40
Martin#7-Jan-10 2:40 
GeneralRe: Valid Reason for Using IDisposable intreface Pin
Gideon Engelberth7-Jan-10 14:54
Gideon Engelberth7-Jan-10 14:54 
Questionget char without enter (console) Pin
sadas232341s5-Jan-10 22:24
sadas232341s5-Jan-10 22:24 
AnswerRe: get char without enter (console) Pin
Hristo-Bojilov5-Jan-10 22:37
Hristo-Bojilov5-Jan-10 22:37 
QuestionC# and Resource File Pin
jojoba20105-Jan-10 22:07
jojoba20105-Jan-10 22:07 
AnswerRe: C# and Resource File Pin
SeMartens5-Jan-10 22:38
SeMartens5-Jan-10 22:38 
QuestionRe: C# and Resource File Pin
jojoba20105-Jan-10 23:05
jojoba20105-Jan-10 23:05 

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.