开发者

working with static variables in .net

开发者 https://www.devze.com 2023-02-14 19:43 出处:网络
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 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..

  1. 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 inform开发者_StackOverflow社区ation in static variables.

  2. 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.


  1. Static variables scope is tied to the Application Domain, therefore static variable value will be the SAME for all users. You can't use static variable to save user specific data. Static variables are good to store application specific information, but as ASP.NET is a multithreaded environment, you need to manage lock/unlock.

  2. How to verify that cookies disabled is described here


  1. I would use session for data that is only required by that user / session and static variables for an application level variables.

What is better: Static variable V.S. Asp.NET Application Session ? seems to agree with my thinking.

  1. if cookies are disabled, does asp.net store the cookie as a session cookie instead or not? as a similar issue talks about explicity requiring cookies or using ViewState as an alternative. You should check if the cookie exists by seeing what is in

    Request.Cookies

and if empty, do whatever alternative you decide on.


Static variables are for the instance of the applications. Form applications run on the clients pc in most cases. And Static variables are used in these all the time.

Web applications only have one instance running on the server. So Every client connected to that server would be sharing the same data.

You use Session variables instead to create variables and objects that pertain to only the client.

0

精彩评论

暂无评论...
验证码 换一张
取 消