Farseer

导航

C# Programming Language 学习笔记(六)

第十章 类
1.A nested type and its containing type have a special relationship with regard to this-access .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
嵌套类型和包含它的类型对于this的访问有特殊的关系.特别地,嵌套类型不能用this引用包含它的类型的实例成员.这种情况下,当嵌套类型需要访问包含它的类型的实例成员时,可以通过把包含它的类型实例的this作为嵌套类型的构造函数的参数.如下面的例子.

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();

    }


}

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 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();

    }


}

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

        }


    }


}


class Test

{

    
static void Main() {

        Derived.Nested n 
= new Derived.Nested();

        n.G();

    }


}

3.Even though constants are considered static members, a constant-declaration neither requires nor allows a static modifier.
尽管常量被当做是静态成员,但是常量的声明不需要也允许用static修饰符.
4.A constant-expression is an expression that can be fully evaluated at compile time. Because the only way to create a non-null value of a reference-type other than string is to apply the new operator, and because the new operator is not permitted in a constant-expression, the only possible value for constants of reference-types other than string is null.
常量表达式必须在编译时求值.因为对于除string以外的所有引用类型创建非null值的唯一办法是用new操作符,又因为new操作服不允许在常量操作符中使用,所以除string以外的所有常量引用类型的值都是null.
5.Specifically, direct assignments to a readonly field are permitted only in the following contexts.

  • In the variable-declarator that introduces the field (by including a variable-initializer in 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 pass a readonly field as an out or ref parameter.

明确地说,给一个readonly的字段赋值只能发生在如下情况下:

1.引入该字段的变量声明(在声明中用变量初始化器)
2.对于实例字段,在包含该字段的类的实例构造器.对于静态字段,在包含该字段的静态构造器.这也是唯一可以把字段作为out或者ref参数传递的情形.


 

posted on 2005-10-28 15:21  佛西亚  阅读(456)  评论(1编辑  收藏  举报