Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.2 Class membersprevious at this levelnext at this level 17.2.6 Nested typesprevious at this levelnext at this level 17.2.6.4 this access Paragraph 11 A nested type and its containing type do not have a special relationship with regard to this-access (§14.5.7). 2 Specifically, this within a nested type cannot be used to refer to instance members of the containing type. 3 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. [Example: 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();  
   }  
}  
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. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS