3

I have the following class:

public class MailData : IDisposable
{
  public IDictionary<String, Tuple<Byte[], String>> Attachments { get; set; }
  public String From { get; set; }
  public IList<String> To { get; set; }   
  public MailType Type { get; set; }
} // MailData

What would be the correct way to dispose the class?

I mean, I think I should remove all Byte[] from Dictionary ...

What about the other properties?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

4 Answers4

4

.NET is a managed environment and you have to allow the Garbage Collector to do it's job, there is no reason for you to dispose simple reference classes and byte[] is still a reference.

Usually you use the Dispose pattern to let go of valuable resources such as file I/O, various Streams, database connections...etc

In your case however, there is no need to do anything on your own.

Welcome to the managed world of C#.

Stan R.
  • 15,757
  • 4
  • 50
  • 58
1

You don't need to do anything, unless your MailType class is also disposable, in which case you'll want to dispose that. You don't need to clear any collections since they will be garbage collected along with their contents some time after they go out of scope.

Lee
  • 142,018
  • 20
  • 234
  • 287
  • 2
    They won't be garbage collected JUST AFTER they go out of scope. The GC will dispose them when it thinks its time to. Just making this clear. – Odys Mar 23 '13 at 13:56
  • they will be available for collection after they go out of scope, when that collection happens is up to the GC. – Stan R. Mar 23 '13 at 13:58
1

There is nothing you should do, unless the MailType has some unmanaged resources. In that case, you should override the Dispose of this class and call the Dispose of the MailType object.

Also, If the amount of bytes in the dictionary is significant, you should wrap the instance in a using statement.

using(var m = new MailData())
{
   // use the object here

} // the m.Dispose() gets called here
Odys
  • 8,951
  • 10
  • 69
  • 111
  • If `MailData` does not implement `IDisposable`, the compiler will reject your attempt to use the `using` statement, no matter how much memory the dictionary takes. I think I agree with what you are trying to say in your answer, but I do not think you are actually saying it. :) –  Mar 23 '13 at 14:03
0

First of all you should consider what methods you need to implement. You can find an interesting article here Summarized: You should only implement dispose / finalize when you really need them, otherwise it will just slow the GC down. In those methods you only need to release unmanaged resources. To learn more about unmanaged resources, have a look at this. Also ask yourself: Am I going to explicitly free the unmanaged resources by calling dispose or am I going to let the GC do it by implicitly calling Finalize.

Hope that helps, Xaser

Community
  • 1
  • 1
Xaser
  • 2,066
  • 2
  • 22
  • 45
  • `IDisposable` can be implemented for classes without a finalizer too, and often is. The question does not ask about a finalizer, and I do not see a reason for assuming one is used. –  Mar 23 '13 at 14:01