c# - Why is there no contract for disposal? -


we have constructors , can treat them contracts follow object instantiation.

there's no other way create instance without providing exact set of parameters constructor.

but how can (and should ever bother) enforce pre-mortem activity? we've got finalizer not recommended general purpose finalization. have idisposable implement. if work disposable object without using have no guarantee dispose ever called.

why there way enforce state of object before let go off?

tiding in finalizer impossible because there's no guarantee object graph intact , referenced object of dying object not nulled gc.

sure, not calling instance object's savestate() client code gives troubles it, not object.

nonetheless considered practice require needed dependencies injected in constructor (if no default value available). nobody readily says: "leave default constructor, create properties , throw exceptions if object in invalid state."

update:

as there many votes closing question i'd design patterns can answer.

whether use di or not can count how many times object requested/created. without explicit release call not know moment when should call dispose.

i not understand how implement disposals @ right time.

why there no way enforce state of object before let go of?

because whole point of garbage collector simulate machine has infinite amount of memory. if memory infinite never need clean up.

you're conflating semantic requirement of program -- particular side effect occur @ particular time -- the mechanisms of simulating infinite storage. in ideal world 2 things should not have each other. unfortunately not live in ideal world; existence of finalizers evidence of that.

if there effects want achieve @ particular time, those effects part of program , should write code achieves them. if important should visible in code people reading code can see , understand them.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -