whats the exact use of static variables in overall programming in .net and for asp.net…
Recently i went for the interview where interviewer asked me 2 question which i was not sure for the same..
-
whats the use of session object, i said sessions are the server side object, they are used when you want to store user specific data at server side, then he asked what if i want to use static variables for the same, i was mum, can anyone tell me how asp.net will behave if i store the user specific information in static variables.
-
If i use cookies which are the best option to store the data at client side (not sensitive one), but what if user has disabled cookies on his machine, will my application would crash, how i have to handle such exception…
1.) Session is a user specific dictionary(every user has it’s own) and static/shared variables are appclication wide(as long as the application runs/all sessions are not abandoned).
Normally you would use Session if you want to store data that belongs to one user and static variables if you want to share it to all users.
Therefore Session is less scalable than static variables because it increases with the number of users. One disadvantage of static variables(or the ASP.Net Cache) is that you have to avoid conflicts when different users access/change them parallel by yourself. Performance is one of the advantage of static variables/cache.
So your anwer on Session was correct. If you want to use static variables for user specific data, you could use a static Dictionary with the User(f.e. the MembershipUser object or its providerkey as the key) and the data you want to store as the value. But normally you would use Session for this as you correctly asnwered.
2.) Determine If a Browser Accepts Cookies
The client can disable cookies. One way to check if cookies are disabled is to write a cookie to the Response and in the next Request to check if the cookie exists. If the cookie doesn’t exists you need to assume that cookies are disabled.
Here are some further informations on cookies.