在参数中声明IDisposable时如何处理它?

时间:2015-12-17 11:02:48

标签: c# .net idisposable

SendEmail("message", "subject", new System.Net.Mail.Attachment(path1), new System.Net.Mail.Attachment(path2));

如何处理参数中的最后两个附件?完成后会自行处理吗?

1 个答案:

答案 0 :(得分:7)

你不能,因为你没有引用它们。

将它们移到方法调用之外:

using (var attachment1 = new System.Net.Mail.Attachment(path1))
using (var attachment2 = new System.Net.Mail.Attachment(path2))
{
    SendEmail("message", "subject", attachment1, attachment2);
}
相关问题