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.

如需組件的詳細資訊,請參閱組件和全域組件快取For more information about assemblies, see Assemblies and the Global Assembly Cache.

內部存取常用於以元件為基礎的開發作業,因為它可讓一組元件私下相互合作,而不會公開給應用程式的其餘程式碼。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()   
   {  
      BaseClass 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:Assembly1.dll  
public class TestAccess   
{  
   static void Main()   
   {  
      BaseClass myBase = new BaseClass();   // Ok.  
      BaseClass.intM = 444;    // CS0117  
   }  
}  

C# 語言規格C# Language Specification

如需詳細資訊,請參閱<C# 語言規格>。 語言規格是 C# 語法及用法的限定來源。

另請參閱See Also

C# 參考C# Reference
C# 程式設計指南C# Programming Guide
C# 關鍵字C# Keywords
存取修飾詞Access Modifiers
存取範圍層級Accessibility Levels
修飾詞Modifiers
publicpublic
privateprivate
protectedprotected