简体   繁体   中英

How can i dispose IDisposable when it is declared in a parameter?

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

How can i dispose the last two attachments in the parameter? Will it dispose itself when done?

You can't, because you don't have a reference to them.

Move them outside the method call:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM