Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable in asp.net page

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

like image 390
Ashwani K Avatar asked Mar 07 '11 09:03

Ashwani K


People also ask

What is static variable in asp net?

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 .

What is static variable with example?

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.

How do you set a static variable?

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.

Is it good to use static variables in C#?

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.


2 Answers

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.

like image 178
Unmesh Kondolikar Avatar answered Oct 06 '22 12:10

Unmesh Kondolikar


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.

like image 41
Adeel Avatar answered Oct 06 '22 12:10

Adeel