Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

10.2.5 Static and instance members

Visual Studio .NET 2003

Members of a class are either static members or instance members. Generally speaking, it is useful to think of static members as belonging to classes and instance members as belonging to objects (instances of classes).

When a field, method, property, event, operator, or constructor declaration includes a static modifier, it declares a static member. In addition, a constant or type declaration implicitly declares a static member. Static members have the following characteristics:

  • When a static member M is referenced in a member-access (Section 7.5.4) of the form E.M, E must denote a type containing M. It is a compile-time error for E to denote an instance.
  • A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.
  • A static function member (method, property, event, operator, or constructor) does not operate on a specific instance, and it is a compile-time error to refer to this in such a function member.

When a field, method, property, event, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member. (An instance member is sometimes called a non-static member.) Instance members have the following characteristics:

  • When an instance member M is referenced in a member-access (Section 7.5.4) of the form E.M, E must denote an instance of a type containing M. It is a compile-time error for E to denote a type.
  • Every instance of a class contains a separate set of all instance fields of the class.
  • An instance function member (method, property, indexer, instance constructor, or destructor) operates on a given instance of the class, and this instance can be accessed as this (Section 7.5.7).

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 (Section 7.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 (Section 7.5.4), instance members must be accessed through instances, and static members must be accessed through types.

Show:
© 2015 Microsoft