c# - Abuse using and Dispose() for scope handling of not to be released objects? -


for convenience , safety reasons i'd use using statement allocation , release of objects from/to pool

public class resource : idisposable {     public void dispose()     {         resourcepool.releaseresource(this);     } }  public class resourcepool {     static stack<resource> pool = new stack<resource>();      public static resource getresource()     {         return pool.pop();     }      public static void releaseresource(resource r)     {         pool.push(r);     } } 

and access pool like

using (resource r = resourcepool.getresource()) {      r.dosomething(); } 

i found topics on abusing using , dispose() scope handling of them incorporate using (blah b = _new_ blah()).
here objects not freed after leaving using scope kept in pool.
if using statement expands plain try dispose() should work fine there more happening behind scenes or chance won't work in future .net versions?

this not abuse @ - common scope-handling idiom of c#. example, ado.net objects (connections, statements, query results) commonly enclosed in using blocks, though of these objects released pools inside dispose methods:

using (var conn = new sqlconnection(dbconnectionstring)) {     // conn visible inside scope     ... } // conn gets released connection pool 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -