X hits on this document

890 views

0 shares

0 downloads

0 comments

217 / 396

C# LANGUAGE SPECIFICATION

class Derived: Base { new public class M { public static void F() { Console.WriteLine("Derived.M.F"); } } }

class Test { static void Main() { Derived.M.F(); } }

shows a nested class M that hides the method M defined in Base.

10.2.6.4 this access

A nested type and its containing type do not have a special relationship with regard to this-access (§‎7.5.7). Specifically, this within a nested type cannot be used to refer to instance members of the containing type. In cases where a nested type needs access to the instance members of its containing type, access can be provided by providing the this for the instance of the containing type as a constructor argument for the nested type. The following example

using System;

class C { int i = 123;

public void F() { Nested n = new Nested(this); n.G(); }

public class Nested { C this_c;

public Nested(C c) { this_c = c; }

public void G() { Console.WriteLine(this_c.i); } } }

class Test { static void Main() { C c = new C(); c.F(); } }

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

Document info
Document views890
Page views890
Page last viewedTue Dec 06 13:26:03 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments