[英]Static properties across different ASP.NET requests
If I create a static property MyLanguage
and one request sets its value to 1
, while at the same time another thread sets it to 2
- what will be the final value of MyLanguage
? 如果我创建一个静态属性
MyLanguage
并且一个请求将其值设置为1
,同时另一个线程将其设置为2
- MyLanguage
的最终值是MyLanguage
?
Does the single MyLanguge
property gets shared across ASP.NET sessions? 是否在ASP.NET会话中共享单个
MyLanguge
属性?
A static property/field is shared across the app domain. 应用程序域之间共享静态属性/字段。 So all your sessions should see the same value .
因此,您的所有会话都应该看到相同的值 。
The only exception is when you use the ThreadStatic attribute on a static field , in which case each thread will see its own value. 唯一的例外是当您在静态字段上使用ThreadStatic属性时,在这种情况下,每个线程都将看到自己的值。 eg
例如
[ThreadStatic]
static int counter = 0; // each thread sees a different static counter.
It would be 2. Static fields, properties are shared between objects.So Latest set values will be updates for all the instances. 它将是2.静态字段,属性在对象之间共享。因此,最新的设置值将是所有实例的更新。
From MSDN 来自MSDN
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object . 使用static修饰符声明一个静态成员,该成员属于该类型本身而不是特定对象 。 The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
static修饰符可以与类,字段,方法,属性,运算符,事件和构造函数一起使用,但不能与索引器,析构函数或类以外的类型一起使用。 For more information
欲获得更多信息
Statics are unique to the application domain, all users of that application domain will share the same value for each static property. 静态对于应用程序域是唯一的,该应用程序域的所有用户将为每个静态属性共享相同的值。
When you see the word static, think "there will only be one instance of this." 当你看到单词static时,请想“只会有一个这样的实例”。 How long that instance lasts is a separate question, but the short answer is that it is variable.
该实例持续多长时间是一个单独的问题,但简短的回答是它是可变的。
If you want to store values specific to the user look into Session State . 如果要存储特定于用户的值,请查看会话状态 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.