internal(C# 参考)internal (C# Reference)

internal 关键字是类型和类型成员的访问修饰符The internal keyword is an access modifier for types and type members.

本页涵盖 internal 访问。This page covers internal access. internal 关键字也是 protected internal 访问修饰符的一部分。The internal keyword is also part of the protected internal access modifier.

只有在同一程序集的文件中,内部类型或成员才可访问,如下例所示: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;
}  

有关 internal 和其他访问修饰符的比较,请参阅可访问性级别访问修饰符For a comparison of internal with the other access modifiers, see Accessibility Levels and Access Modifiers.

有关程序集的详细信息,请参阅 .NET 中的程序集For more information about assemblies, see Assemblies in .NET.

内部访问通常用于基于组件的开发,因为它可使一组组件以私有方式进行协作,而不必向应用程序代码的其余部分公开。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. 例如,用于生成图形用户界面的框架可以提供 ControlForm 类,这两个类通过使用具有内部访问权限的成员进行协作。For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate by 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.

示例Example

此示例包含两个文件,即 Assembly1.csAssembly1_a.csThis example contains two files, Assembly1.cs and Assembly1_a.cs. 第一个文件包含内部基类 BaseClassThe first file contains an internal base class, BaseClass. 在第二个文件中,尝试实例化 BaseClass 会产生错误。In the second file, an attempt to instantiate BaseClass will produce an error.

// Assembly1.cs  
// Compile with: /target:library  
internal class BaseClass   
{  
   public static int intM = 0;  
}  
// Assembly1_a.cs  
// Compile with: /reference:Assembly1.dll  
class TestAccess   
{  
   static void Main()   
   {  
      var myBase = new BaseClass();   // CS0122  
   }  
}  

示例Example

在此示例中,使用在示例 1 中所用的相同文件,并将 BaseClass 的可访问性级别更改为 publicIn this example, use the same files you used in example 1, and change the accessibility level of BaseClass to public. 另将成员 intM 的可访问性级别更改为 internalAlso 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;  
}  
// Assembly2_a.cs  
// Compile with: /reference:Assembly2.dll  
public class TestAccess   
{  
   static void Main()   
   {  
      var myBase = new BaseClass();   // Ok.  
      BaseClass.intM = 444;    // CS0117  
   }  
}  

C# 语言规范C# Language Specification

有关详细信息,请参阅 C# 语言规范中的声明的可访问性For more information, see Declared accessibility in the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also