Considering I have simple class, that I just want to initialize (or do some task once in a lifetime of the object). Does switching the reference to Task.Completed after all is done change anything, considering GC?
public class EmployeesAtPremisesAfterWork
{
private ConcurrentHashSet<int> isAtPremisesAfterWork = new ConcurrentHashSet<int>();
public Task WaitUntilInitialized;
public EmployeesAtPremisesAfterWork(SomeInitializationHelper helper)
{
WaitUntilInitialized = Task.Run(async () =>
{
var attachedValues = await helper.GetEmployeeAttachedValues();
isAtPremisesAfterWork.AddRange(attachedValues.AtPremisesAfterWork);
WaitUntilInitialized = Task.CompletedTask;
});
}
}
I was just curious, since I still am not really sure about things GC related. Does Task keep reference to its context, after it is finished, or does it release it?
New contributor
Petr Vejchoda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.