Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.2 Class membersprevious at this levelnext at this level 17.2.5 Static and instance members Paragraph 11 Members of a class are either static members or instance members. [Note: Generally speaking, it is useful to think of static members as belonging to classes and instance members as belonging to objects (instances of classes). end note] Paragraph 21 When a field, method, property, event, operator, or constructor declaration includes a static modifier, it declares a static member. 2 In addition, a constant or type declaration implicitly declares a static member. 3 Static members have the following characteristics: Paragraph 31 When a field, method, property, event, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member. 2 (An instance member is sometimes called a non-static member.) 3 Instance members have the following characteristics: [Example: The following example illustrates the rules for accessing static and instance members:
class Test  
{  
   int x;  
   static int y;  
   void F() {  
      x = 1;      // Ok, same as this.x = 1  
      y = 1;      // Ok, same as Test.y = 1  
   }  
   static void G() {  
      x = 1;      // Error, cannot access this.x  
      y = 1;      // Ok, same as Test.y = 1  
   }  
   static void Main() {  
      Test t = new Test();  
      t.x = 1;     // Ok  
      t.y = 1;     // Error, cannot access static member through  
      instance  
      Test.x = 1;   // Error, cannot access instance member through type  
      Test.y = 1;   // Ok  
   }  
}  
The F method shows that in an instance function member, a simple-name (§14.5.2) can be used to access both instance members and static members. The G method shows that in a static function member, it is a compile-time error to access an instance member through a simple-name. The Main method shows that in a member-access (§14.5.4), instance members must be accessed through instances, and static members must be accessed through types. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS