Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
 internal

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
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 (C# Reference) and Access Modifiers (C# Programming Guide).

For more information about 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 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.

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 by using C#.

This example contains two files, Assembly1.cs and Assembly1_a.cs. The first file contains an internal base class, 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
   }
}

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;
}
// Assembly2_a.cs
// Compile with: /reference:Assembly1.dll
public class TestAccess 
{
   static void Main() 
   {
      BaseClass myBase = new BaseClass();   // Ok.
      BaseClass.intM = 444;    // CS0117
   }
}

For more information, see the following sections in the C# Language Specification:

  • 3.5.1 Declared Accessibility

  • 3.5.4 Accessibility Constraints

  • 10.3.5 Access Modifiers

  • 10.3.8.2 Declared Accessibility

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Does it has to be in the same namespace?      Itay Levin ... Thomas Lee   |   Edit   |   Show History

If I declare to Classes in the same assembly but on diffrent namespaces...

does the internal modifier still allowing me to access internal members of one class on the other class?

[tfl - 22 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker