zoukankan      html  css  js  c++  java
  • internal (C# Reference)

    The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly, as in this example:

    public class BaseClass 
    {
        
    // Only accessible within the same assembly
        internal static int x = 0;
    }


    For a comparison of internal with the other access modifiers, see Accessibility Levels and Access Modifiers (C# Programming Guide).

    For more information on assemblies, see Assemblies and the Global Assembly Cache (C# Programming Guide).

    A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.

    It is an error to reference a type or a member with internal access outside the assembly within which it was defined.

    NoteNote

    An internal virtual method can be overridden in some languages, such as textual Microsoft intermediate language (MSIL) using Ilasm.exe, even though it cannot be overridden using C#.

    Example

    This example contains two files, Assembly1.cs and Assembly2.cs. The first file contains an internal base class, BaseClass. In the second file, an attempt to instantiate BaseClass will produce an error.


    代码
    Copy

    // Assembly1.cs
    // compile with: /target:library
    internal class BaseClass 
    {
       
    public static int intM = 0;
    }

    Copy

    // Assembly1_a.cs
    // compile with: /reference:Assembly1.dll
    class TestAccess 
    {
       
    static void Main() 
       {
          BaseClass myBase 
    = new BaseClass();   // CS0122
       }
    }




    In this example, use the same files you used in example 1, and change the accessibility level of BaseClass to public. Also change the accessibility level of the member IntM to internal. In this case, you can instantiate the class, but you cannot access the internal member.


    代码
    // Assembly2.cs
    // compile with: /target:library
    public class BaseClass 
    {
       
    internal static int intM = 0;
    }

    Copy

    // Assembly2_a.cs
    // compile with: /reference:Assembly1.dll
    public class TestAccess 
    {
       
    static void Main() 
       {
          BaseClass myBase 
    = new BaseClass();   // Ok.
          BaseClass.intM = 444;    // CS0117
       }
    }



     Reference: http://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.80%29.aspx


  • 相关阅读:
    四、元数据ParameterMetaData
    三、PreparedStatement对象(重点)
    二、properties文件存储信息
    一、JDBC3.0标准中常用接口与类
    十一、MySQL 数据库存储引擎介绍
    十、执行计划
    注册表中一些项目拒绝访问,拒绝更改权限,拒绝删除
    x64dbg 的条件断点及断点日志
    手机APP无法抓包HTTPS解决方案
    原版黑苹果安装(个人备忘)
  • 原文地址:https://www.cnblogs.com/sandy_liao/p/1891490.html
Copyright © 2011-2022 走看看