因此,我一直在使用System.Net.Mail.MailMessage
對象通過SmtpClient
發送電子郵件一段時間。我注意到MailMessage
實現了IDisposable
,所以我總是在using
塊中使用它。爲什麼System.Net.Mail.MailMessage實現IDisposable
using(MailMessage msg = new MailMessage())
{
msg.To = blah... etc;
...
smtpclient.Send(msg);
}
從元數據,你可以看到的MailMessage
// Summary:
// Releases all resources used by the System.Net.Mail.MailMessage.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Dispose();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.Mail.MailMessage
// and optionally releases the managed resources.
//
// Parameters:
// disposing:
// true to release both managed and unmanaged resources; false to release only
// unmanaged resources.
protected virtual void Dispose(bool disposing);
實施此信息,但我想知道,爲什麼MailMessage
實現IDisposable
?它似乎沒有任何與網絡相關的項目,因爲SmtpClient
處理所有這些。
它可能是由於可能保存附件的文件句柄?我忘了還有什麼嗎?
附件需要關閉,一個。 – 2011-12-13 23:05:50