Chapter 10
1. A nested type and its containing type have a special relationship with regardThis-Access. Specifically,ThisWithin 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 providingThisFor the instance of the containing type as a constructor argument for the nested type. The following example
The nested type has a special relationship with the type containing it for this access. in particular, the nested type cannot use this to reference instance members of its type. in this case, when the nested type needs to access the instance members of the type containing it, you can use this of the type instance containing it as the parameter of the nested Type constructor. the following is an example.
Using System;
Class C
{
Int I = 123 ;
Public Void F () {
Nested n= NewNested (This);
N. G ();
}
Public Class Nested {
C this_c;
Public Nested (c) {
This_c=C;
}
Public Void G () {
Console. writeline (this_c. I );
}
}
}
Class Test {< br> static void main () {< br>
C = New C ();
C. F ();
}
}
2. A nested type has access to all of the Members that are accessible to its containing type, including members of the containing type that havePrivateAndProtectedDeclared accessibility. The example
The embedded type can access all Members that can be accessed by its type, including private and protected members of its type, as shown below:
Using System;
Class C
{
Private Static Void F () {
Console. writeline ("C. f");
}
Public Class Nested
{
Public Static VoidG (){
F ();
}
}
}
Class Test
{
Static VoidMain (){
C. nested. G ();
}
}
A nested type also may access protected members defined in a base type of its containing type. In the example
The embedded type can also access the protection members of the base type that contains its type. For example:
Using System;
Class Base
{
Protected VoidF (){
Console. writeline ("Base. f");
}
}
Class Derived: Base
{
Public Class Nested
{
Public Void G () {
Derived d= NewDerived ();
D. F ();//OK
}
}
}
Class Test
{
Static Void Main () {
Derived. nested n= NewDerived. nested ();
N. G ();
}
}
3. Even though constants are considered static members, Constant-Declaration Neither requires nor allows Static Modifier.
Although constants are regarded as static members, static modifiers are not required for constant declaration.
4.Constant-expression Is an expression that can be fully evaluated at compile time. Because the only way to create a non-null value of Reference-type Other String Is to apply New Operator, and because New Operator is not permitted in Constant-expression , The only possible value for constants Reference-types Other String Is Null .
Constant expressions must be evaluated during compilation. because the only way to create non-null values for all reference types except string is to use the new operator, and because the new operator is not allowed in the constant operator, therefore, values of all constant reference types except string are null.
5. Specifically, direct assignments to Readonly Field are permitted only in the following contexts.
-
InVariable-declaratorThat introduces the field (by includingVariable-initializerIn the Declaration ).
-
For an instance field, in the instance constructors of the class that contains the field Declaration; for a static field, in the static constructor of the class that contains the field declaration. these are also the only contexts in which it is valid to passReadonlyField asOutOrRefParameter.
Specifically, assigning a value to a readonly field can only happen in the following situations:
1. Introduce the variable declaration of this field (use the variable initialization tool in the Declaration)
2. for instance fields, the instance constructor contains the fields. for static fields, in the static constructor that contains this field. this is the only case where the field can be passed as an out or ref parameter.