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;
    }
}
mikha eAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
mikha eAuthor 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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

käµfm³d 👽Commented:
Yes, ASP.NET is multi-threaded, and each request gets its own thread.
0
mikha eAuthor 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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.