I’ve seen developers use two approaches when they are done with a session object.
1) Session.Remove(key)
2) Session(key) = nothing
What is the better approach of the two above? Does Session.Remove dispose the object stored automatically?
Does the second approach depend on the garbage collector to actually free up the memory?
EDIT:
Thanks for the responses, guys. Seems like Session.Remove is the correct way to go. However, if Session.Remove does not guarantee the disposal of object, then what is the best way to dispose the object stored in session when we do not need it further?
Thanks.
If you set the object to
nullor nothing, the key still exists in the Session even though the value isnull.This behaviour is the same as any other dictionary type classes in the CLR.
If you want to remove an object completely from the Session, you should use the
Session.Remove("key")method.It wont throw an exception if the key is not present in the Session.