简体   繁体   English

asp.net/C#中的静态变量

[英]static variables in asp.net/C#

I'm using extensively static variables in my web application project. 我在我的Web应用程序项目中使用了大量的静态变量。 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). 现在我从一些文章中读到它是整个项目的全局变量,静态变量中的数据可以被其他用户共享或覆盖(我的意思是它不是用户特定的或特定于会话的)。

So is it general programming practice not to use static variables in the normal web application development? 那么通常的编程习惯是不在常规Web应用程序开发中使用静态变量吗?

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? 是否完全没有使用静态变量,就像GOTO语句/关键字一样,这意味着使用它们有很多限制,最好不要使用它们? Then in what cases do we use the static key word? 那么在什么情况下我们使用静态关键字?

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 ? 那么我有这个要求,特定变量必须在特定的webform.aspx.cs中初始化一次,并且范围必须仅限于那个特定的.aspx.cs和已经登录的特定用户? How do i meet this requirement ? 我如何满足这一要求? If possible can any one illustrate this with 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. 它们使代码难以进行单元测试,并且由于并发访问和竞争条件而导致细微错误。

As far as your requirement is concerned you could use store the variable as a property of the control in the ViewState . 就您的要求而言,您可以使用将变量存储为ViewState中控件的属性。 If it is user specific data that you are trying to store then you could use the Session state . 如果它是您尝试存储的用户特定数据,则可以使用会话状态

I believe your interpretation of static is wrong. 我相信你对静态的解释是错误的。

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. 使用static修饰符声明一个静态成员,该成员属于该类型本身而不是特定对象。

In other words, there is only one instance of this member for all the specific instances of the class. 换句话说,对于该类的所有特定实例,此成员只有一个实例。

There isn't anything wrong with static variables as long as you use them correctly. 只要正确使用静态变量,静态变量就没有任何问题。 I believe you are mixing static with global variables. 我相信你将静态与全局变量混合在一起。 Global variables can be accessed from everywhere. 可以从任何地方访问全局变量。 This isn't desireable since knowing when and where the state of that variable is set is complex. 这是不可取的,因为知道设置该变量的状态的时间和地点是复杂的。 Furthermore this makes unit testing more difficult. 此外,这使得单元测试更加困难。


This Programmers.SE question probably interests you. 这个Programmers.SE问题可能对你很感兴趣。

On static's, there are various reasons why they should be avoided in general, although they do have their specific uses. 在静态上,尽管它们具有特定的用途,但是为什么它们应该在一般情况下被避免有各种原因。

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 ? 那么我有这个要求,特定变量必须在特定的webform.aspx.cs中初始化一次,并且范围必须仅限于那个特定的.aspx.cs和已经登录的特定用户? How do i meet this requirement ? 我如何满足这一要求? If possible can any one illustrate this with code ? 如果可能,任何人都可以用代码说明这一点吗

For this requirement, I would suggest you look at clarifiying the requirement: 对于这个要求,我建议你看看澄清要求:

Personally I prefer using Session - with ViewState it's very easy for things to go wrong, and when they do go wrong it can be very hard to debug! 我个人更喜欢使用Session - 使用ViewState它很容易出错,当它们出错时,调试起来非常困难!


explanation of: "when they do go wrong it can be very hard to debug" - ViewState can be configured to works several ways, but generally it's set up to work by serializing objects into the client pages as Hidden form fields and then subsequently deserializing these objects when the page PostBack occurs. 解释:“当它们出错时,它可能很难调试” - ViewState可以配置为多种方式,但通常它设置为通过将对象序列化为客户端页面作为隐藏表单字段然后反序列化这些发生页面PostBack时的对象。 I've spent many many days debugging a certain DNN based website which had "Invalid ViewState" problems only on some browsers, only on some pages and only some of the time. 我花了很多天时间调试某个基于DNN的网站,该网站仅在某些浏览器上出现“无效ViewState”问题,仅在某些页面上,而且仅在某些时间。 What caused this? 是什么造成的? After several days I still didn't know... hence why I stay clear of ViewState if I can. 几天后我仍然不知道...因此,如果可以的话,为什么我要远离ViewState。 However, I admit that this might be an unfair decision - in my case, I was working with a lot of third party code which generated dynamic pages and which created a lot of ViewState (size and complexity of ViewState is actually one of my reasons for not using WebForms at all if I can). 但是,我承认这可能是一个不公平的决定 - 在我的情况下,我正在使用大量第三方代码生成动态页面并创建了大量ViewState(ViewState的大小和复杂性实际上是我的原因之一)如果可以的话,根本不使用WebForms)。

例如,如果你有一些服务,那么你可以将它用作静态,因为不需要IIS为服务创建重复的对象,因为它们都是相同的:)

怎么样使用会话..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM