X hits on this document

1399 views

0 shares

0 downloads

0 comments

218 / 396

Chapter ‎18   Unsafe code

shows this technique. An instance of C creates an instance of Nested and passes its own this to Nested’s constructor in order to provide subsequent access to C’s instance members.

10.2.6.5 Access to private and protected members of the containing type

A nested type has access to all of the members that are accessible to its containing type, including members of the containing type that have private and protected declared accessibility. The example

using System;

class C { private static void F() { Console.WriteLine("C.F"); }

public class Nested { public static void G() { F(); } } }

class Test { static void Main() { C.Nested.G(); } }

shows a class C that contains a nested class Nested. Within Nested, the method G calls the static method F defined in C, and F has private declared accessibility.

A nested type also may access protected members defined in a base type of its containing type. In the example

using System;

class Base { protected void F() { Console.WriteLine("Base.F"); } }

class Derived: Base { public class Nested { public void G() { Derived d = new Derived(); d.F();// ok } } }

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

Document info
Document views1399
Page views1399
Page last viewedMon Jan 23 01:31:20 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments