且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在.NET中使用后,设置对象为Null /没有

更新时间:2023-10-14 22:39:58

卡尔是绝对正确的,也没有必要使用后,设置对象为null。如果一个对象实现的IDisposable ,只要确保你叫 IDisposable.Dispose()当你与该对象就大功告成了(包在尝试 .. 最后,或者,一个使用()块)。但是,即使你不记得叫做的Dispose(),在对象上finaliser方法应该调用的Dispose()给你。

Karl is absolutely correct, there is no need to set objects to null after use. If an object implements IDisposable, just make sure you call IDisposable.Dispose() when you're done with that object (wrapped in a try..finally, or, a using() block). But even if you don't remember to call Dispose(), the finaliser method on the object should be calling Dispose() for you.

我认为这是一个良好的治疗效果:

I thought this was a good treatment:

挖掘到了IDisposable

Understanding IDisposable的

没有试图第二猜测GC和它的管理策略,因为它是自调整和不透明的任何一点。有关于与杰弗里里希特内部运作上点网岩在这里商量好了:杰弗里里希特Windows内存模式和 通过C#第20章有一个伟大的处理Richters书 CLR:

There isn't any point in trying to second guess the GC and its management strategies because it's self tuning and opaque. There was a good discussion about the inner workings with Jeffrey Richter on Dot Net Rocks here: Jeffrey Richter on the Windows Memory Model and Richters book CLR via C# chapter 20 has a great treatment: