在99%的案例中,Microsoft s框架大为克服,正确做法简单:
- If your class has any fields of types that implement IDisposable, and nobody s going to expect to use those objects once you re done with them, you should implement IDisposable, and your disposal method should call Dispose on all such fields.
- If your class has no such fields, but you think classes that derive from yours might, or if your class needs to implement an interface like IEnumerator(of T) which requires IDisposable, you should have an overridable Dispose method that does nothing.
- The proper semantics for a "dispose" method is for an object to do whatever is necessary to clean up other objects before it is abandoned. If an object doesn t have to clean up other objects, Dispose should harmlessly do nothing. There is never any reason to throw NotImplementedException or NotSupportedException from Dispose.
执行可转让的识别资料的关键点是,清理任何特定类型的“资源”,而是确保如果系统的一个物体改动 ,其他>/i” 实体需要一段时间清理,这些实体将获得清理,而这样做所需的信息和动力仍然存在。 理想的情况是,这种清理应尽快进行,但更早。 如果某一物体包含如一阵列,则无需清理。 扼杀不需要任何清理;一系列不需要清理的物体不需要任何清理,而只持有未要求清理的其他物体的物体也需要清理。 另一方面,像开张笔记本这样的行动,需要确保采取某种清理行动(填表)。 如果某一物体打开了提纲并保存了有关材料,则要求对该物体进行处置的目的就是要销毁有关袖珍的资料(将由垃圾收集者负责),但要确保在提纲上开展必要的“客户”行动。