
17 Classes
17.2 Class members
17.2.6 Nested types
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]
| |
| Jagger Software Ltd | |
| Company # 4070126 | |
| VAT # 762 5213 42 |