在asp.net/C#静态变量 [英] static variables in asp.net/C#

查看:172
本文介绍了在asp.net/C#静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Web应用程序项目使用广泛的静态变量。现在,我已经从一些文章,这是整个项目的全局变量,那就是在静态变量可以共享或其他用户改写的数据读取(我的意思是不特定用户或会话特定的)。

I'm using extensively static variables in my web application project. Now I have read from some articles that it is a global variable for the whole project and the data that is in the static variables can be shared or overwritten by other users (I mean it is not user specific or session specific).

因此​​,它是一般的编程惯例是不正常的Web应用程序开发中使用静态变量?

So is it general programming practice not to use static variables in the normal web application development?

是静态变量根本不使用,就像GOTO语句/关键字含义有大量的限制,使用它们,并在所有pferably不使用$ P $?那么在何种情况下我们使用静态关键字?

Are static variables not used at all just like GOTO statement/keyword meaning there are extensive restrictions to use them and preferably not used at all? Then in what cases do we use the static key word?

然后,我有此要求的特定变量,必须在特定的webform.aspx.cs只有一次初始化和范围必须被限制为只对特定.aspx.cs以及谁已经登录该特定用户吗?我如何满足这一要求?如果可能的话任何一个可以用code说明这一点?

Then i have this requirement that a particular variable has to be initialized only once in a particular webform.aspx.cs and the scope has to be restricted to only to that particular .aspx.cs and to that particular user who has logged in ? How do i meet this requirement ? If possible can any one illustrate this with code ?

推荐答案

我个人尽量避免静态变量尽可能。它们使code难以进行单元测试,也可能引入微妙的错误,由于并发访问和比赛条件。

Personally I try to avoid static variables as much as possible. They make the code difficult to unit test and also could introduce subtle bugs due to concurrent access and race conditions.

至于您的要求而言,你可以使用变量存储为在一个控件的属性ViewState的。如果你正在尝试存储,那么你可以使用会话状态用户特定的数据。

As far as your requirement is concerned you could use store the variable as a property of the control in the ViewState. If it is user specific data that you are trying to store then you could use the Session state.

这篇关于在asp.net/C#静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆