13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATION17.2.4 Constituent typesTypes that are used in the declaration of a member are called the constituent types of that member. Possibleconstituent types are the type of a constant, field, property, event, or indexer, the return type of a method oroperator, and the parameter types of a method, indexer, operator, or instance constructor. The constituent types ofa member must be at least as accessible as that member itself (§10.5.4).17.2.5 Static and instance membersMembers of a class are either static members or instance members. [Note: Generally speaking, it is useful tothink of static members as belonging to classes and instance members as belonging to objects (instances ofclasses). end note]When a field, method, property, event, operator, or constructor declaration includes a static modifier, itdeclares a static member. In addition, a constant or type declaration implicitly declares a static member. Staticmembers have the following characteristics:• When a static member is referenced in a member-access (§14.5.4) of the form E.M, E must denote a type thathas a member 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 specificinstance, 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 staticmodifier, 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 is referenced in a member-access (§14.5.4) of the form E.M, E must denote aninstance of a type that has a member 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 agiven instance of the class, and this instance can be accessed as this (§14.5.7).[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 = 1static void G() {x = 1;y = 1;// Error, cannot access this.x// Ok, same as Test.y = 1}static void Main() {Test t = new Test();t.x = 1; // Okt.y = 1;Test.x = 1;// Error, cannot access static member through instance// Error, cannot access instance member through typeTest.y = 1; // Ok}}The F method shows that in an instance function member, a simple-name (§14.5.2) can be used to access bothinstance members and static members. The G method shows that in a static function member, it is a compile-timeerror to access an instance member through a simple-name. The Main method shows that in a member-access214

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!