Click here to Skip to main content
15,893,722 members
Home / Discussions / C#
   

C#

 
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 
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 
Sorry for the delay in responding - I forgot about it.

1) It tells the user of the class that instances should be disposed.

A programmer familiar with .NET will immediately recognize and understand what pattern is involved and the reasons for it just by glancing at the class declaration.

2) It allows code to test programmatically if an object is disposable and to invoke the logic without knowledge of it's type.

if (obj is IDisposable) ((IDisposable)obj).Dispose();

3) It allows encapsulation of the object's resources.

In your case, and in many cases, you may need to expose the image to outside users (rather than clone the internal resource and return a copy). But in general, a class should be responsible for managing all the state belonging to it. Following this guideline greatly simplifies life even if you cannot always *enforce* it by fully encapsulating state.

4) It allows the user to easily ensure instances are in fact disposed, whether or not exceptions occur, since several CLS-compliant languages (including C# and VB.NET) have special statements for this exact purpose. In C#,

using (var x = new MyThing()) // MyThing is IDisposable
{
   ... code block ...
}

// is equivalent to

var x = new MyThing();
try
{
   ... code block ...
}
finally
{
   if (x != null) x.Dispose();
}



All of these are, in my opinion, important reasons for implementing IDisposable when the object is in fact disposable. Having x.img.Dispose() violates established OOP practice (and there are good reasons for that practice).
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 
AnswerRe: C# and Resource File Pin
SeMartens5-Jan-10 23:21
SeMartens5-Jan-10 23:21 
GeneralRe: C# and Resource File Pin
jojoba20106-Jan-10 0:01
jojoba20106-Jan-10 0:01 
AnswerRe: C# and Resource File Pin
carlecomm6-Jan-10 18:16
carlecomm6-Jan-10 18:16 
QuestionDraw a line avoiding objects Pin
Zaegra5-Jan-10 21:45
Zaegra5-Jan-10 21:45 
GeneralRe: Draw a line avoiding objects Pin
Skippums6-Jan-10 7:58
Skippums6-Jan-10 7:58 
Questiondatatable filter Pin
abcurl5-Jan-10 21:38
abcurl5-Jan-10 21:38 

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.