internal 修饰符 备忘

.internal 修饰符起什么作用?

答:

internal 修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问接口的成员不能使用 internal 修饰符值得注意的是,如果为 internal 成员加上了 protected 修饰符,这时的访问级别为 internal 或 protected。只是看字面意思容易弄错,许多人认为 internal protected 应该是“只有同一个程序集中的子类可以访问”,但其实它表示“同一个程序集中的所有类,以及所有程序集中的子类都可以访问”

示例

Example05Lib 项目的 Class1 

 

Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Example05Lib
{
public class Class1
    {
internal String strInternal = null;
public String strPublic;
internal protected String strInternalProtected = null;
    }
}

 

结果
Example05Lib 项目的 Class2 类可以访问到 Class1 的 strInternal 成员,当然也可以访问到 strInternalProtected 成员,因为他们在同一个程序集里

 

Example05 项目里的 Class3 类无法访问到 Class1 的 strInternal 成员,因为它们不在同一个程序集里。但却可以访问到 strInternalProtected 成员,因为 Class3 是 Class1 的继承类

Example05 项目的 Program 类既无法访问到 Class1 的 strInternal 成员,也无法访问到 strInternalProtected 成员,因为它们既不在同一个程序集里也不存在继承关系

 

5.internal 修饰符起什么作用?

答:

internal 修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问接口的成员不能使用 internal 修饰符值得注意的是,如果为 internal 成员加上了 protected 修饰符,这时的访问级别为 internal 或 protected。只是看字面意思容易弄错,许多人认为 internal protected 应该是“只有同一个程序集中的子类可以访问”,但其实它表示“同一个程序集中的所有类,以及所有程序集中的子类都可以访问”

示例

Example05Lib 项目的 Class1 

 

Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Example05Lib
{
public class Class1
    {
internal String strInternal = null;
public String strPublic;
internal protected String strInternalProtected = null;
    }
}

 

结果
Example05Lib 项目的 Class2 类可以访问到 Class1 的 strInternal 成员,当然也可以访问到 strInternalProtected 成员,因为他们在同一个程序集里

 

Example05 项目里的 Class3 类无法访问到 Class1 的 strInternal 成员,因为它们不在同一个程序集里。但却可以访问到 strInternalProtected 成员,因为 Class3 是 Class1 的继承类

Example05 项目的 Program 类既无法访问到 Class1 的 strInternal 成员,也无法访问到 strInternalProtected 成员,因为它们既不在同一个程序集里也不存在继承关系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值