可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know that when you use a static variable it's value is shared across all users.
static string testValue = "";
protected void SomeMethod(object sender, EventArgs e)
{
testValue = TextBox1.Text;
string value = TestClass.returnString(TextBox1.Text); // <-- return from a static method
}
So in this case, if one user goes to a website and puts a value into the textbox, the string testValue
will be overwritten by another value when another user enters something in the textbox. (I think?)
I now have this class:
public class TestClass
{
public static string returnString(string msg)
{
return msg;
}
}
My question is: if I use a static method, is the return value of that method shared for all users as well? or is that always a "unique" value per user?
Let's say that this method is called five times, by five different users, will this static method return the value a particular user has entered, or is it possible that one user gets a value that another user entered?
回答1:
Your question was:
my question now is, if i use a static method, is the return value of
that method shared for all users aswell? or is that always a "unique"
value per user ?
And the answer is, it depends. Using your example:
public class TestClass
{
public static string returnString(string msg)
{
return msg;
}
}
In this case, 5 different users would (most likely) pass in 5 different strings to the static method. Therefore they would get back five differents strings. So for this case:
string value = TestClass.returnString(TextBox1.Text);
each user would get back whatever they had typed into their own TextBox. If, on the other hand the code was this:
string value = TestClass.returnString(testValue);
They would all get back what happened to be in the static string at the time of the return.
So the rule to keep in mind is:
- If the method uses static member variables, there is a risk of different users affecting each other's results. This is true regradless of whether the method is static or not.
- If the method uses only call parameters and local variables, and the call parameters are not themselves pointing to statisc member variables, calls from different users will not affect each other.
回答2:
A static method is not bound to a specific object, but rather to a class. You don't need an instance of TestClass
to call the static function declared inside your class. Therefore a static function cannot use non-static members of a class, as they are bound to a specific instance of the class.
In fact this is the same for static variables. A static variable is not bound to a specific object, but to a class. If one object changes the variable, it is changed for all objects, as it exists only once.
回答3:
A static method or variable is always available trough the class itself and is not only bind to an instance of this class.
回答4:
my question now is, if i use a static method, is the return value of
that method shared for all users aswell? or is that always a "unique"
value per user ?
Static variables are shared within an application domain. The web server could be running multiple instances side-by-side, sharing them across multiple users so you cannot rely on it.
One instance per user: Use session variables if you want to maintain unique value per user.
One instance for all users: It is not trivial to share a truly single unique variable instance accross multiple users, consider using a database to ensure that all users are getting the same value.
回答5:
If the static method uses no shared resources, there will be no issue with multiple callers hitting it at the same time. So as long as the method is not passed "shared objects" as parameters (meaning each caller has their own instance of the passed paremeter(s)), and the static method itself doesn't use any shared resources (variables/objects etc...that are scoped outside of the static method), it's as if each caller has their own copy of the method because they each run the method on their own thread.
However, if the static method uses shared objects or resources (such as static objects defined elsewhere), multiple callers could step on each other so you would need some kind of locking in the static method to handle this. In this case the static method would not be thread-safe unless you added your own logic to handle multiple threads hitting it.
回答6:
As far as I understand, the 5 users who are logged on to the site from 5 different locations or from five different computers, this value will not be shared. It will only be shared for one user from one PC.
You can assume that each browser runs a separate copy of the application. Static variables are not shared across Applications but within an Application.
回答7:
You're just returning the variable that has been passed as an argument, so your question doesn't make sense.
Each method-call will take another variable as an argument, and that variable will get returned, so how can that variable be shared amongst all users ?
回答8:
Please dont rely on static variables but static methods are commonly use when need get a Single instance of an objetc example the HttpContext