X hits on this document

1082 views

0 shares

0 downloads

0 comments

215 / 396

C# LANGUAGE SPECIFICATION

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

10.2.6 Nested types

A type declared within a class or struct is called a nested type. A type that is declared within a compilation unit or namespace is called a non-nested type.

In the example

using System;

class A { class B { static void F() { Console.WriteLine("A.B.F"); } } }

class B is a nested type because it is declared within class A, and class A is a non-nested type because it is declared within a compilation unit.

10.2.6.1 Fully qualified name

The fully qualified name (§‎3.8.1) for a nested type is S.N where S is the fully qualified name of the type in which type N is declared.

10.2.6.2 Declared accessibility

Non-nested types can have public or internal declared accessibility and have internal declared accessibility by default. Nested types can have these forms of declared accessibility too, plus one or more additional forms of declared accessibility, depending on whether the containing type is a class or struct:

202Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1082
Page views1082
Page last viewedSun Dec 11 06:38:01 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments