Reputation: 81721
I am working with Asp.NET and I have a class named PathFinder
which has methods like StyleBarPath(string BrandId,string ProductId)
which returns for example a combination of path including brandId
and productId
and also there are such methods in the same class.
What I am thinking to make them static
methods to invoke them easily in every where by saying PathFinder.StylePath("1","2");
to use the returned values inside a <a href=""></a>
user control.
But since I am working too much these days, what I know is getting complicated for some reasons. Anyways, here is my question :
Since I am using inline coding on a lot of places like <a href='<%=PathFinder.StylePath("1","2")%>'></a>
, I don't want to create a lot of instances by doing this <a href='<%=new PathFinder().StylePath("1","2")%>'></a>
by declaring the methods not-static.
But I am afraid of changing the methods returns values because defining the methods static. I mean when a client calls this method, it wouldn't affect the other clients who invoke the same method at the same time?
They would have different call stacks right?
Lets say :
{brandId:2,productId:3}
{brandId:3,productId:4}
This actions happens near the same time when the server processing their requests. What I want to learn is whether the invocations affect each others and change the returning values of each other since they are defined static.
Thanks for reading so far and being a helper :)
I just don't want the clients see path/1/2/
while they are waiting for path/2/3/
Some Notes about the question :
Upvotes: 3
Views: 885
Reputation: 719
This should answer most of you questions http://odetocode.com/Articles/313.aspx
As I understand it, static methods are thread safe, but not static properties
Upvotes: 1
Reputation: 821
In C#, static means (in layman's terms) there is one copy. If you provide arguments to the method, and you return a value based on those parameters only, then you will be perfectly safe.
The only time you might run into problems is if your static method uses any static variables to store data between calls, since that could make call-1 change the value of call-2.
From the example you gave, you are safe.
As for your question about static fields, each caller can see the same static fields, since those are shared between instances. Keep that in mind while programming!
Upvotes: 1
Reputation: 22719
You can call a static method safely from multiple threads simultaneously and at separate times given the following:
If your method is what it looks like it is, you're simply taking some inputs adjusting them and returning the result without accessing anything outside of the function. That would be completely safe.
Static fields are not the same as methods at all. It is one copy of a variable. Any changes to that static field will affect everything else that uses it.
Upvotes: 7