Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static properties across different ASP.NET requests

Tags:

c#

asp.net

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?

Does the single MyLanguge property gets shared across ASP.NET sessions?

like image 338
Your father Avatar asked Feb 01 '26 04:02

Your father


2 Answers

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. e.g.

[ThreadStatic]
static int counter = 0; // each thread sees a different static counter.
like image 109
Eren Ersönmez Avatar answered Feb 03 '26 21:02

Eren Ersönmez


It would be 2. Static fields, properties are shared between objects.So Latest set values will be updates for all the instances.

From MSDN

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. 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. For more information

like image 45
Rajesh Subramanian Avatar answered Feb 03 '26 21:02

Rajesh Subramanian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!