In dotnet, if I create a (HashSet<T> disposable) where T : IDisposable
and store the hashSet object in a field. When the Disposable
objects will be disposed? Consider this code:
public class LongRunningProcess: ServiceBase
{
private HashSet<IDisposable> set;
public LongRunningProcess()
{
set = new HashSet<IDisposable>();
}
void Process()
{
using (var disposable= new IDisposable() )
{
if (!set.Contains(disposable))
//Process
else
set.Add(disposable);
}
}
}
In this code I have created a class that holds onto the HashSet field, because it is a worker service it never dies (until next restart). Does that imply that the hashset’s members will be disposed of at restart? Considering that hashsets are a set of hashes does their lifespan affects the lifespan of their member object?