I have this static method on C# :
public class MyClass
{
public MyClass()
{
}
public static void myOwnMethod(int myValue)
{
int internalValue;
internalValue=myValue+10;
Console.Write(internalValue);
}
}
now, if user A call MyClass.myOwnMethod(), could user B call the same method called by user A (is static) or when user B call that method it like to create another "instance" of class/method?
Because could be dangerous have static methods so…
EXAMPLE
- userA call MyClass.myOwnMethod(10);
- if meanwhile the Console.Write of userA userB call MyClass.myOwnMethod(20), which are the results for each user?
userA get 20 and userB 30 or both get 30?
Yes, of course.
But what exactly happens depends. Are your ‘Users’ threads or do they run separate instances of the program?
After the Update
When you call a static method for 2+ users (requests) in a server application then the inside of that method should be thread-safe. In the example that is the case because
internalValueis a local variableConsole.Write()is thread-safe by design.But in a ‘real’ situation you would be tempted to use share (static) data, and then you would need some form of locking.