Summary: in this tutorial, you’ll how to use the C# internal
keyword to restrict types and their members to be accessible within the same assembly.
Introduction to C# internal keyword
In .NET, an assembly is a package of code and resources that the .NET runtime can deploy, version, and execute, and developers can use to create applications or libraries.
To create an assembly from code, you compile the code into DLL
or EXE
file. Then, you or other developers can reference the assembly from another project.
C# internal
keyword specifies that types like classes and interfaces or members of types should be accessible only within the same assembly, also known as assembly scope. In other words, if a type or member of a type is marked as internal
, other assemblies cannot access it directly.
The internal
keyword allows you to effectively hide implementation detail. It also ensures that your library is used as intended, without exposing the internal details to external assemblies. This makes your code more maintainable and secure.
Let’s say you develop a C# library called MyLib
that contains a Utility
class that you want to use in your projects within the same solution. But you don’t want other developers to access the Utility
class and its methods from their assemblies outside of your library.
To do it, you can use the internal
keyword for the Utility
class and its method like this:
namespace MyLib
{
internal class Utility
{
internal static void Calculate()
{
}
}
class MyClass
{
public static void DoSomething()
{
Utility.Calculate();
// ...
}
}
}
Code language: C# (cs)
In this example, we mark the Utility
class and its Calculate()
method as internal
. It means that we only allow access to them within the same assembly (MyLib
project).
On the other hand, we mark the MyClass
as public
, meaning that we allow other assemblies to reference the MyClass
.
It’s important to note that if you attempt to access the Utility
class or its Calculate()
method from another assembly, you’ll get a compilation error.
Protected Internal
The protected internal
access modifier is a combination of the protected
and internal
modifiers. The protected internal
allows access to members from within the same assembly, as well as from within derived classes in any assembly, more specifically:
- A
protected internal
member can be accessed from any class within the same assembly, just like aninternal
member. - A
protected internal
member can also be accessed from a derived class in any assembly, just like aprotected
member.
Here’s an example of using the protected internal
access modifier:
// Assembly1
namespace MyLib;
public class MyBaseClass
{
protected internal int MyProtectedInternalField;
}
public class MyDerivedClass: MyBaseClass
{
public void DoSomething()
{
var value = MyProtectedInternalField;
Console.WriteLine(value);
}
}
Code language: C# (cs)
In this example, the MyBaseClass
has a protected internal
member. Hence, we can access it from the DoSomething()
method of the derived class within the same assembly.
// Assembly2
using MyLib;
namespace MyLib2;
public class MyClass : MyDerivedClass
{
public void MyMethod()
{
// reference the protected internal field
// from the MyDerivedClass in another assembly
var value = MyProtectedInternalField;
//...
}
}
Code language: C# (cs)
In this assembly, we define the MyClass
that extends the MyDerivedClass
class from Assembly1
.
The MyMethod()
of the MyClass
can access the MyProtectedInternalField
member of the MyDerivedClass
of the Assembly1
.
Summary
- Use the C#
internal
keyword to allow types and their members to be accessible within the same assembly. - Use he
protected internal
modifier to allow a member to be accessed within the same assembly, as well as from the derived class from the same or another assembly.