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?
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.
A static method or variable is always available trough the class itself and is not only bind to an instance of this class.