c# - How to Implement Dispose In Inheriting Class -


i using smtpclient in .net-3.5 framework (meaning not implement idisposable), put in using statement so:

using (var client = new disposablesmtpclient("mail.domain.com", 25) { credentials = new networkcredential(), enablessl = false }) {     client.send(emailaddress,                 emailaddress,                 subject,                 body); } 

so created following:

class disposablesmtpclient : smtpclient, idisposable {     bool disposed;       public disposablesmtpclient(string mailserver, int port) : base(mailserver, port)     {         // var client = new smtpclient(mailserver, port);     }      public void dispose()     {         this.dispose();         gc.suppressfinalize(this);     } } 

which works fine sending message, when debugging, throws stackoverflow exception (as this.dispsoe(); call forever).

i tried calling this.dispose(true) per many other questions, complains no overload method 'dispose' takes 1 arguments.

base.dispoe() work, because of course 'smtpclient' not conatin definition 'dispose'

finally, tried signature protected override void dispose(bool disposing), dispose(): no suitable method found override

is able point me in right direction this?

1 - don't need dispose anything, don't need dispose.

just go new:

var client = new smtpclient("mail.domain.com", 25) { credentials = new networkcredential(), enablessl = false };  client.send(emailaddress,             emailaddress,             subject,             body); 

2 - if really want use using (and don't know why should), have implement empty dispose (because don't have dispose begin with):

class disposablesmtpclient : smtpclient, idisposable {     public disposablesmtpclient(string mailserver, int port) : base(mailserver, port)     {     }      public void dispose()     {         // do, don't anything.     } } 

imo, kiss principle should consider.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -