I am having one doubt regarding the use of static variable in Asp.net pages.
I am having one page say UserDetails.aspx. In this page, I have one static variable to store some data specific to a user. So, will this variable be shared across multiple user or a separate variable will be created for each user?
public partial class UserDetails : System.Web.UI.Page
{
static int numberOfReviews=0;
protected void Page_Load(object sender, EventArgs e)
{
numberOfReviews= GetReviews();
}
}
Here, will numberOfReviews be specific to each user or will be shared?
numberOfReviews
Static variables are values that are also available across the whole application, but have some performance advantages and fewer overheads. The main difference to static variables and the previous examples is the fact that static variables are a side benefit of ASP.NET being object-oriented and the global being a .
1) A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. For example, we can use static int to count a number of times a function is called, but an auto variable can't be used for this purpose.
Static variable If a variable is static , the variable is assigned memory once and all objects of the class access the same variable. A static variable can be created by adding the static keyword before the variable during declaration.
Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.
Application Scope: The variables that have application scope are available throughout the application, i.e to all users of the applications across all pages.
Session Scope: When many users connect to your site, each of them will have a separate session (tied to the identity of the user that is recognized by the application.) When the variable has session scope it will have new instance for each session, even though the users are accessing the same page. The session variable instance is available across all pages for that session.
Page Scope: When you have a instance variable on a Page it is specific to that page only and that session only.
Static variables have Application scope
. All users of the application will share the same variable instance in your case.
Please note that although static variables have one instance in the app domain. So if you have your application deployed on a load balanced web farm, each app domain will have a separate instance of the variable. This might give you incorrect result.
Based on this you should decide what scope your variable should be in. IMO, using static variables is a code smell and should be discouraged.
static variable scope is application wide. numberOfReviews
will be shared among all users. you need to use Session
to store per user, so it is accessible in all pages. On the other hand, if you just need it on a specific page, you can save it in ViewState
and can get it in post back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With