I created a class which contains a list<> object.
After adding records to the list<> and closing / reopening the app I noticed that the records were still contained in the list.
How long will this persist?
Is that an ASP.NET app? So did you restarted IIS? IS that an static class?
This is for a .net web site.
I'm not viewing this as a bad thing.
I'm actually intrigued by it.
public class Customer{private int _id;private string _name;private string _city;private string _phone;private string _state;public Customer(){//// TODO: Add constructor logic here//}public Customer (int id,string name,string city,string phone,string state) { Id = id; Name = name; City = city; Phone = phone; State = state; }public int Id {get {return _id; }set { _id =value; } }public string Name {get {return _name; }set { _name =value; } }public string City {get {return _city; }set { _city =value; } }public string State {get {return _state; }set { _state =value; } }public string Phone {get {return _phone; }set { _phone =value; } }}////////////////////////////////////
public class CustomerList{private static List custList =new List ( );public CustomerList(){//// TODO: Add constructor logic here//}public List Select ( ) {return custList; }public Customer SelectSingle (int custID ) {if ( custID == -1 )return null;return custList.Find (delegate ( Customer customer ) {return customer.Id == custID; } ); }public void Update ( Customer c ) {int localID = c.Id; Customer e = custList.Find(delegate (Customer customer) {return customer.Id == c.Id; }); e.Id = c.Id; e.Name = c.Name; e.City = c.City; e.State = c.State; e.Phone = c.Phone; }public void Insert ( Customer c ) { custList.Add ( c ); }public void Delete ( Customer deleteCustomer ) { Customer customerFound = custList.Find (delegate ( Customer customer ) {return customer.Id == deleteCustomer.Id; } ); custList.Remove ( customerFound ); }}
mpaterson:
private static List custList =new List ( );
remove the static word and you won't see this problem. As the List will stay in memory until you remove it or IIS resets it.
So I could use something like this for a shopping cart functionality until checkout when I read the data to the DB?
I wish I new about this sooner!
Is the case with all static variables?
What about static methods? How would that work?
Static classes only have one instance, so they are shared between all sessions. Now when they are not used anymore the GC will take care of them, so better to add it into a Cache variable to avoid that.
0 comments:
Post a Comment