I am working on an existing project, setup by another coder. I'm having some trouble understanding how state is being maintained between pages. There is a Class library which has some helper objects. Mostly these objects are just used for there static methods and rarely instantiated or inherited.
This is an example class I'm testing with.
public sealed class Application
{
public static string Test;
}
Now when i run something like the following in the base class of my page, I would expect the result to be "1: 2:Test" all the time (note that "1" is empty), but strangly its only this way the first time it is run. Then every time afterwards its "1:Test 2:Test". Somehow its maintaining the state of the static variable between pages and being refreshed??
Response.Write("1开发者_如何转开发:" + SharedLibrary.Application.Test);
SharedLibrary.Application.Test = "Test";
Response.Write(" 2:" + SharedLibrary.Application.Test);
I need to create more classes like this, but want to understand why this is occurring in the first place.
Many Thanks
As correctly stated by others: Anything declared as static variable in an Asp.Net application will keep its same value until changed.
Here is what you really need to know:
This value will be lost when (not if, but when) the application pool recycles. Usually this happens daily, but also happens if you run out of memory or otherwise crash.
This value will be accessed by all the requests concurrently, i.e. multiple users, etc.
So if this value is a counter for example:
Response.Write("1:" + SharedLibrary.Application.Test);
SharedLibrary.Application.Test = int.Parse(SharedLibrary.Application.Test) + 1;
Response.Write(" 2:" + SharedLibrary.Application.Test);
You might find unexpected output such as "1: 53 2: 55". This would be a result of executing your test page at the same time from a couple of browsers.
Static variables always initialize when you first access or create an instance of the class. They all initialize. So if you have a large "bucket" o' static variables (I call badly designed helper classes "buckets"), and you only use one of them, the memory for each one is still allocated at that time.
Finally, this value is never garbage collected. So if you put a large item in the static variable, then it will consume that memory as long as the process lives. (Please do not put Disposable resources, i.e. SqlConnection's, in there.)
In ASP.Net, static variable keep their state as long as the server is running (with a few exceptions). If you want to have a static variable that doesn't keep it's state across page refreshes(read: HTTP requests) then wrap a property around the HttpContext
class.
Most web applications/frameworks actually keep the code running between invocations, and only stop it when the server or worker is shut down. PHP is the odd one out here, not everyone else.
A static class or static member once loaded (accessed) remains in memory until the application that loaded it, is unloaded. So, when your page requests access the static member SharedLibrary.Application.Test
each request is accessing the same memory location.
The MSDN article Static Classes and Static Class Members will give you some additional insight.
When you say you need to create more class like this, what is it that you want these classes to do?
精彩评论