Domanda

I am storing list of customers in session. When I reset, I wanted to know what happens to the previous cached listed, will it be destroyed automatically. Since I no longer need it, is it possible to destroy it completely and store a new value.

public IList<Customer> CachedCustomers
{
  set 
  { 
     HttpContext.Current.Session["Customers"] = null;//<-- Is this line required
     HttpContext.Current.Session["Customers"] = value; 
  }
}
È stato utile?

Soluzione

No, setting to null is not needed. This is no different than any variable or field in .NET. Setting to value x, then setting to value y is the same as setting to value y.


Of course, this would not apply to a property, which could run code on every set, and could do something different between setting to null and setting to something else.

Altri suggerimenti

For the Add session:

 HttpContext.Current.Session["Customers"] = value; 

For Remove the Session:

 HttpContext.Current.Session.Contents.Remove("Customers") 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top