简体   繁体   中英

Best practice to store values in session

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; 
  }
}

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.

For the Add session:

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

For Remove the Session:

 HttpContext.Current.Session.Contents.Remove("Customers") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM