Question about static classes in ASP.Net 2.0

J

JC

Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?
 
M

Masudur

Dear JC.

when you create A static class. Only One copy remain in the Application
so far this is clear.
But do they share same copy or do they have different copy of them? The
answer is Yes they share the same copy of a static class.

try the sample code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Application["yahoo"] == null)
{
Random r = new Random();
Application.Add("yahoo", r.NextDouble());
}
Response.Write(TestClass.GetString());
}

}

public static class TestClass
{
public static string yahoostring = "Munna";
static TestClass()
{
Random r = new Random();
HttpContext.Current.Application["yahoo"] = r.NextDouble();
}
public static string GetString()
{
string s = yahoostring +
HttpContext.Current.Application["yahoo"].ToString();
return s;
}
}

Thanks
Md. Masudur Rahman
www.kaz.com.bd
KAZ Software Ltd.
Software outsourcing made simple...
 
L

Laurent Bugnion

Hi,
Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?

Actually, additionally to Masudur's answer, the static members or
classes are shared not only throughout the Web application, but
throughout the process. Since all the web applications run inside one
single process, it means that static classes and members are shared
between different web applications running on the same server. This is
why you have to handle them with a lot of care.

If you want to save variables inside the web application, but not share
them with other web applications, you use the Application collection
(similar to the Session collection).

HTH,
Laurent
 
G

Guest

This is close but not correct. Static things are 1 per AppDomain (as
marshalling is too expensive across them to do it by default).
ASP.net starts each web application in its own app domain, therefore .net
web applications will not share static objects.

Ciaran O'Donnell
 
L

Laurent Bugnion

Hi,
This is close but not correct. Static things are 1 per AppDomain (as
marshalling is too expensive across them to do it by default).
ASP.net starts each web application in its own app domain, therefore .net
web applications will not share static objects.

Ciaran O'Donnell

Oops I stand corrected. What confused me is that when you run a Web
application, static variables remain allocated until you restart the
ASP.NET process. I misinterpreted the consequences. Sorry for that.

Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top