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?
Your question was:
And the answer is, it depends. Using your example:
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:
each user would get back whatever they had typed into their own TextBox. If, on the other hand the code was this:
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: