C#中成员的可见性

C#提够的访问修饰符有:private,public,protected,internal,protected internal
1.成员(方法,字段,构造函数)可见性选项:
class  SomeClass
    
{
        
//可以从任何地方访问
        public void PublicMethod()
        
{

        }

        
//仅可以从SomeClass类型中访问
        private void PrivateMethod()
        
{
            
throw new System.NotImplementedException();
        }

        
//可以再SomeClass类和它的派生类中访问
        protected void ProtectMethod()
        
{
            
throw new System.NotImplementedException();
        }

        
//可以在同一个程序集中访问
        internal void InternalMethod()
        
{
            
throw new System.NotImplementedException();
        }

        
//在程序集中受保护的访问
        protected internal void ProtectInternalMethod()
        
{
            
throw new System.NotImplementedException();
        }


        
//没有标记的成员在C#中默认为私有的
        void SomeMethod()
        
{
            
throw new System.NotImplementedException();
        }


现在假设创建一个SomeClass类的实例并尝试用点(.)运算来调用每一个方法
static   void  Main(String[] args)
        
{
            SomeClass c 
= new SomeClass();
            c.PublicMethod();
            c.InternalMethod();
            c.ProtectInternalMethod();
            c.PrivateMethod();                  
//错误!!!
            c.ProtectMethod();                //错误!!!
            c.SomeMethod();                  //错误!!!
        }
 
如果编译这个程序,你会发现受保护的和私有的成员都不能通过对象来访问!!!
2.设置类型可见性:
类型(类,接口,结构,枚举,委托)也可以带访问修饰符,但只能是pubilc或internal,其中internal是类型默认的访问修饰符

转载于:https://www.cnblogs.com/dushouke/archive/2008/04/23/1168321.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值