Best way to clear a value from a ThreadLocal<T>
I am using a ThreadLocal<T> to store an item-per-thread, but every soft often, I wish to clear a thread's stored value. How is the best way of doing this? I can reset the instance's value to null, but the IsValueCreated property still shows true.
var storage = new ThreadLocal<string>(); storage.Value = "hello"; //storage.IsValueCreated -> true storage.Value = null; //storage.IsValueCreated -> still true
You can't de-initialize the data for a single thread. There is no method to do it (other than recreating the ThreadLocal<>, losing all the values)
Once assigned, the value of IsValueCreated is true for that thread. null is a perfectly ok value, it doesn't mean "deassign the value" or "the value is not assigned".