How are static variables handled in asp.net

  • Thread starter Frank Wisniewski
  • Start date
F

Frank Wisniewski

I use static variables in my asp.net apps instead of application variables
to store global parameters, its worked well for me so far but I was
wondering if it is efficient. Does anyone know how static variables are
handled in asp.net?
 
S

Scott Allen

Hi Frank:

If the static variables are readonly, then they can be much friendlier
than using the Application collection. For one, you don't have the
overhead of locking that the Application collection does for you, and
secondly they are type safe and you don't have to do any casting or
worry about boxing.

If you need to read and write to static variables take care, because
ASP.NET is a multithreaded environment. The Application collection
does locking for you during a Get or a Set with a reader / writer
lock, but using statics you are on your own.
 
F

Frank Mamone

How do you use static variables?

Is that like global variables that are always available?

-Frank
 
K

Karl

I agree with everything Scott said.

If you are after readonly values, you should also consider using a
configuration file, namely the web.config with your own configuration
section (http://www.openmymind.net/Configuration/index.html). Its good
practice to closely examine hard-coded to see if they should be, well
hard-coded.

As Scott said, you can run into nasty stuff if you are reading and writing
to and from them. If this is the case, i would definitely avoid static
variables...I would also avoid the Application object, and use a database
with Caching (I hate the application object ;) )

Karl
 
S

Scott Allen

Hi Frank:

If I have this class in an ASP.NET project:

public class Foo
{
public static string Bar
{
get
{
// pull a string from the config file,
// the database, a resource file, etc..
}
}
}

Then I can write:

Foo.Bar

from anywhere else in code to pull back the string I need. Public
static members are global and always available (which can make them a
double edged sword in some scenarios).

VB.NET works the same way, except the keyword is "Shared" instead of
static.
 
F

Frank Mamone

Thanks!

Scott Allen said:
Hi Frank:

If I have this class in an ASP.NET project:

public class Foo
{
public static string Bar
{
get
{
// pull a string from the config file,
// the database, a resource file, etc..
}
}
}

Then I can write:

Foo.Bar

from anywhere else in code to pull back the string I need. Public
static members are global and always available (which can make them a
double edged sword in some scenarios).

VB.NET works the same way, except the keyword is "Shared" instead of
static.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top