问题
From the MSDN docs on list.Clear()
Count is set to 0, and references to other objects from elements of the collection are also released.
From what I've been taught (which could be wrong), disposed & released are different things. Disposed means the items is completely removed from memory, released means it's simply not bound to that list by pointer.
Does that mean that I need to do
foreach (var item in Items)
{
item.Dispose();
}
this.Items.Clear();
If I effectively want to completely destroy/clear/release/dipose a list from existence?
To me it should all be as simple as .Clear()
, but, it's a bit unclear to me (pun intended) if that is enough and correct.
Note that we are not overriding Dispose()
nor Clear()
, it's all default implementation.
Also, the list is a List<T>
.
Edit : As per comment recommendation, I've went and checked, the items are all IDisposables
, which sheds some light on the fact that they should be disposed
Duplicate clarification on What is IDisposable for? :
I do not think these two questions are the same, mine is asking a difference between two things, the other is asking for clarification on one of those things. It also did not appear in my search before I decided to write the question, because I was too focused on keywords like "difference dispose vs clear", which will probably also be the case for future developers looking for an answer. I'll concede that the other answer provides some more information and is a good link to add here.
回答1:
From what I've been taught (which could be wrong), disposed & released are different things.
True.
Disposed means the items is completely removed from memory, released means it's simply not bound to that list by pointer.
Wrong. Disposed means that any clean-up required by an object that has nothing to do with managed memory is done.
If the elements are IDisposable
then it is indeed a good idea to Dispose()
them all before clearing, if they are generally reached through that list and hence nothing else will Dispose()
them.
If they aren't IDisposable
this not only isn't needed, but it's not possible.
If they are IDisposable
but something else will still be using the elements, then you shouldn't Dispose()
them as that will break that other use.
回答2:
No, List<T>.Clear does not dispose objects. If you want you can write an extension:
public static class IEnumerableExtensions
{
public static void DisposeAll<T>(this IEnumerable<T> seq) where T : IDisposable
{
foreach (T d in seq)
d?.Dispose();
}
}
var connections = new List<SqlConnection>();
// ...
connections.DisposeAll();
回答3:
As far as I know, Dispose()
is very helpful in some areas like if you're handling resource and you want to clear those resource in the memory or if there's any exception happens, surely you need to dispose that.
Just a tip: You can basically don't need to include Dispose()
method when you're using statement code this like
using(var someResource = new SomeResource()){
// Some galaxy logic here
}
Since it has Dispose mechanism. If those resources are not manage of CLR so just use using
statement will be an easy option.
来源:https://stackoverflow.com/questions/49428888/do-i-need-to-dispose-items-in-a-c-sharp-list-before-clearing-it