static in C#

My understanding of static class or member is that , we have only one copy.
consider following class with static members, say I have multiple instances of class Foo and I try to call static methods or access static member.  doesn't it run into race condition?
or in other words , do we have to worry about race conditions, when we create static class or static members?


var one = new Foo();
var two = new Foo();
var three = new Foo();

public class Foo{
    private static string = "foo";
   
    public int GetCount() {
       //return count;
    }
}
novice programmerAsked:
Who is Participating?
 
käµfm³d 👽Commented:
IMO, it should be rare that you have static variables in ASP.NET coding. If you do end up with such, then you should strive for read-only. Are you really ever going to have some piece of data that is shared among every single request to your page? Don't get me wrong:  you might, but I suspect that more often than not you won't. If you do what you can to avoid static variables, then you lessen the work you have to do to protect those variables. Only shared resources need to be protected when working with multi-threading.

Also to be clear:  static methods aren't inherently dangerous. The's nothing dangerous about Math.Min. But if static methods read/write static (i.e. shared between threads) data, then you have a problem.
1
 
Kyle AbrahamsSenior .Net DeveloperCommented:
some helpful understanding:
https://www.cis.upenn.edu/~matuszek/cit591-2006/Pages/static-vs-instance.html

To answer your question, yes, race conditions are a worry as any instance updating the static variable should affect the other instances.

Note though that you could only access the static member by the class name, not by the instance name.

            Foo foo = new Foo();
            Foo foo2 = new Foo();

            Foo.x = "test";
            MessageBox.Show(Foo.x);

    public class Foo
    {
        public static string x = "foo";

    }

Open in new window

1
 
käµfm³d 👽Commented:
do we have to worry about race conditions
If you're going to multi-thread, then yes. Otherwise, there's only one thread, and there can't be a race condition with only one thread.
1
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
novice programmerAuthor Commented:
thanks @käµfm³d and Kyle Abrahams.

what about ASP.NET MVC applications. does each http request have their own thread or how does it work ?
0
 
käµfm³d 👽Commented:
Yes, ASP.NET is multi-threaded, and each request gets its own thread.
0
 
novice programmerAuthor Commented:
thanks @käµfm³d . one last clarification then. I see many helper static classes/methods written by developers in ASP.NET projects, without making it thread safe. isn't this a issue then? or am i not understanding this right? or is it a problem when we are accessing/modifying static variables in those static methods?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Note that the session is per user though . . . and so if you're modifying the session (HttpContext.Current.Session) within a static class that should not be an issue.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.