Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does static variable work in ASP.NET page?

I had an interview today and every thing was going very good, but then an interviewer asked me a question Where Does Static Variable Work in C#- At Application Level or At Page Level.

I was not very much clear about this answer as I only knew that static variables are stored on heap and I didn't knew anything about web related thing.

Then he tried to make me more clear by giving an example that in a page I am using static variable and three users are accessing the page one of the user updates the value of static variable, What value will be visible to remaining two users an old copy or the update will be reflected.

like image 455
Shantanu Gupta Avatar asked Apr 26 '10 16:04

Shantanu Gupta


People also ask

Where do static variables go?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

How can we use static variable in ASPX page?

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.

Where do we use static variable 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.

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 .


2 Answers

Unless it's [ThreadStatic], a static variable will only have one value for each AppDomain.

In ASP.Net, each application has its own AppDomain, so static variables will be shared by all requests in the application. This is what the interviewer was getting at – using static variables in ASP.Net applications is a common mistake that can lead to mysterious corruption errors when multiple requests happen at once.

like image 172
SLaks Avatar answered Oct 04 '22 17:10

SLaks


After one page changes the value, the other pages would all get the updated value.

This may or may not be what you want. This is why static variables are dangerous in web programming. In a Winforms application for example, a static variable works fine to store values that are global for this process, as there is likely just one process running. You get the expected behavior.

In a web application however, your code can be started in multiple threads in the same AppDomain. Developers are sometimes surprised when the value is shared.

If you want the values to be different (you usually do), you can force this using the ThreadStatic attribute. Different web request are in different threads, so they will remain ignorant of each other. I never use this since I don't trust garbage collection to get rid of the value before the next page call, which might reuse the same thread. Likewise, I wouldn't trust static variables for purposefully sharing values between asp.net threads; use a server variable.

like image 32
Patrick Karcher Avatar answered Oct 04 '22 17:10

Patrick Karcher