use of static modifier  
Author Message
MSP.Saami





PostPosted: Visual C# General, use of static modifier Top

hi,

consider I am creating a class with all it members as STATIC. And If, now I am using the class in my application..I am calling some Methods in it...OK..Consider now ClientA calls a method...simultaneously ClientB calls the same method..I want to know, in this case whether the same copy of the method handles both clients or a separate copy. I think , as I am using STATIC , it would maintain only one copy. Am I right or not .

So ,if I want to maintain a separate copy of data for different clients, then I should use normal class declaration without STATIC, am I right or not

your suggestions pls..

thanx..




Visual C#12  
 
 
Mattias Sjogren





PostPosted: Visual C# General, use of static modifier Top

There's only one copy of the method's code in memory (that's true for both static and instance methods). But each invocation of the method gets its own stack frame where local variable data is stored.

 
 
Figo Fei - MSFT





PostPosted: Visual C# General, use of static modifier Top

hi,

As a method, it has one copy for execution whatever its declaration is static or not.

And for other members, static ones are shared with all objects instantiated from the class as "common property", which belongs to the class itself rather than to a specific object, while normal members of a class are in the certain space of the specific class instance assigned by runtime.

A static member cannot be referenced through an instance. Instead, it is referenced through the type name.

For example, consider the following class:

public class MyBaseC 
{
  public struct MyStruct 
  {
    public static int x = 100;
  }
}

To refer to the static member x, use the fully qualified name (unless it is accessible from the same scope):

MyBaseC.MyStruct.x
for more: http://msdn2.microsoft.com/en-us/library/79b3xss3(VS.80).aspx