c# - Creating new object then reallocating -


a question builds upon to "new" or not "new"

foo object1 = new foo(); // operations later ... object1 = new foo(); 

is i'm attempting advisable? , if foo implements idispoable, need call dispose before calling new operator second time?

yes, you're doing in example fine.

the object1 variable reference object of type foo. after first new assignment refers 1 particular instance of foo; after second new assignment refers different instance (and original instance becomes eligible garbage collection, assuming there's nothing else referencing it).

and yes, if foo implements idisposable should dispose it, preferably using using block, although personal preference use separate using variables each block:

using (foo first = new foo()) {     // }  using (foo second = new foo()) {     // else } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -