views:

174

answers:

7

Can a single process with multiple threads cause a static class to be created more than once?

If I just need a simple construct can I use a static class, or do I have to resort to a singleton?

A: 

You can use a static class.

tster
+2  A: 

Can a single process with multiple threads cause a static class to be created more than once?

No, multiple threads will all point to the same static class.

If I just need a simple construct can I use a static class, or do I have to resort to a singleton?

Don't really understand your question. But general you use Singleton when you need to have global data access. You can get the same thing with static class with static members. But that makes the code uglier because there are just too many statics keywords and makes your life difficult should you want to discard the global access strategy and use a more OOP approach.

Singleton, however, is a much preferred method, compared to static class.

Edit: You may want to read this: static classes are not thread-safe

Ngu Soon Hui
You have already answered my question :)For the particular problem I am working on I need to access one resource globally between two threads. I got them signalling to each other with a named EventWaitHandle, but then needed to send a string from one to the other. I placed that in the static class in a Queue that is lock() { } protected to retrieve by the other thread, but then was not sure if 2 threads would get the same static class.
My Other Me
A: 

You can't create an instance of a static class... But a static instance of a normal class will be the same over all threads

Zenuka
+2  A: 

I think when you say "created" you really mean "instantiated," instantiation being the creation of an object of that class. Static classes are never instantiated. They are essentially a collection of global variables and functions.

One instance of each static member variable is created at runtime, and they will be shared among all threads.

Eddie Sullivan
I disagree. Static classes do get instantiated as soon as anything tries to access them. They have their own static constructors in which you can define your own logic.
My Other Me
Despite the strange and confusing naming decision behind "static constructors", there is no instance of the static class being created. The class is being loaded, initialized, used, but *not* "instantiated".
Sixten Otto
+1 YES... Static classses are not instantiated. The Type structure created once for every type used in your application is NOT an instance, or an instaiation, of the Type... It is a Type definition structure. @myotherm, This Type structure (called CORINFO_CLASS_STRUCT), contains the Runtimetype information. When an object is instantiated, the object header contains a pointer to this thing.
Charles Bretana
OK. I learned something new again.
My Other Me
+6  A: 

Yes a static class can act as a singleton-like construct. All the static members it contains will only exists once - but per AppDomain (just keep in mind that one process can have multiple (isolated) .NET AppDomains).

bitbonk
Don't forget about the ThreadStaticAttribute.
GvS
How would one process have more than 1 AppDomain?
My Other Me
+1, AppDomains.
meklarian
@myotherme - a process could have more than one AppDomain by explicitly creating them (using one of the AppDomain.CreateDomain overloads). See this topic for more info: http://stackoverflow.com/questions/786923/when-would-i-use-an-appdomain.
Jeff Sternal
-1: because although technically answer is yes, the answer in this case should be NO, unless the process has multiple AppDomains... etc..
Charles Bretana
@Charles - the 'yes' was answering the original poster's second question "If I just need a simple construct can I use a static class."
Jeff Sternal
+1 for specifying "per AppDomain". I learned that the hard way.
dboarman
+1  A: 

Can a single process with multiple threads cause a static class to be created more than once?

Irrespective of the number of threads, the only way to get more than one instance of static .NET class (members) in a process is by making new AppDomains. Each AppDomain will then have one instance of the static class (members).

AppDomain Class @ MSDN

edit:

I should add that the number of threads in a process and the number of AppDomains are separate as well. A single-threaded process create any number of AppDomains if it wished. Also, it is only up to program flow whether any extra threads call into additional AppDomains. This could create a situation where multiple threads are accessing more than one instance of a static class, but it won't happen unless you explicitly create this scenario yourself.

If I just need a simple construct can I use a static class, or do I have to resort to a singleton?

It seems unclear what you're trying to ask here. Static class members represent singletons by concept. If you're asking whether this is a good method of creating a simple construct, the answer is maybe. It depends on what your needs are. Most will argue that you should just use a regular class definition and just make one instance.

Given your two questions in conjunction, I would speculate that maybe you are asking if you can create only one instance as a singleton by using a static class. The answer is yes, as you won't automatically have new AppDomains unless your code explicitly creates one or if you import code that creates new AppDomains using your assemblies.

meklarian
A: 

Can a single process with multiple threads cause a static class to be created more than once?

Yes you can, though it's somewhat unusual to do (or even want to). It requires either a separate AppDomain, or use of ThreadStaticAttribute. Either of those will give you a separate instance that does not share state with other static instances.

If I just need a simple construct can I use a static class, or do I have to resort to a singleton?

You can use either. A singleton allows you to use a non-static class like a static, and gives you some additional flexibility in managing lifetime.

Mark Brackett