Setting Objects to Null/Nothing after use in .NET

后端 未结 15 2283
囚心锁ツ
囚心锁ツ 2020-11-22 16:09

Should you set all the objects to null (Nothing in VB.NET) once you have finished with them?

I understand that in .NET it is essential to

15条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 16:49

    There are some cases where it makes sense to null references. For instance, when you're writing a collection--like a priority queue--and by your contract, you shouldn't be keeping those objects alive for the client after the client has removed them from the queue.

    But this sort of thing only matters in long lived collections. If the queue's not going to survive the end of the function it was created in, then it matters a whole lot less.

    On a whole, you really shouldn't bother. Let the compiler and GC do their jobs so you can do yours.

提交回复
热议问题