Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why do we use internal keyword in C#?
The internal keyword in C# is an access modifier that provides assembly-level access. It allows class members to be accessible within the same assembly but restricts access from other assemblies, making it ideal for creating APIs that should only be used internally within your project.
Syntax
Following is the syntax for declaring internal members −
internal class ClassName {
// class accessible within assembly only
}
class MyClass {
internal int field; // field accessible within assembly
internal void Method() { // method accessible within assembly
// implementation
}
}
How Internal Access Works
The internal access modifier allows members to be accessed from any class or method within the same assembly, but prevents access from external assemblies. This provides a middle ground between public (accessible everywhere) and private (accessible only within the same class).
Using Internal for Class Members
Example
using System;
namespace RectangleApplication {
class Rectangle {
internal double length;
internal double width;
double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class Demo {
static void Main(string[] args) {
Rectangle rc = new Rectangle();
rc.length = 10.35; // internal field accessible within assembly
rc.width = 8.3; // internal field accessible within assembly
rc.Display();
}
}
}
The output of the above code is −
Length: 10.35 Width: 8.3 Area: 86.005
Using Internal for Entire Classes
Example
using System;
internal class DatabaseHelper {
internal string ConnectionString { get; set; }
internal void Connect() {
Console.WriteLine("Connecting to database...");
}
}
public class UserService {
private DatabaseHelper dbHelper = new DatabaseHelper();
public void ProcessUser() {
dbHelper.ConnectionString = "Server=localhost;";
dbHelper.Connect();
Console.WriteLine("User processed successfully");
}
}
class Program {
static void Main(string[] args) {
UserService service = new UserService();
service.ProcessUser();
}
}
The output of the above code is −
Connecting to database... User processed successfully
Access Modifier Comparison
| Access Modifier | Same Class | Same Assembly | Different Assembly |
|---|---|---|---|
private |
? | ? | ? |
internal |
? | ? | ? |
public |
? | ? | ? |
Common Use Cases
-
Helper classes that should only be used within your library or application.
-
Internal APIs that provide functionality for your assembly but shouldn't be exposed publicly.
-
Data access layers where database connection details should remain internal to the data layer.
-
Configuration classes that manage settings within a specific assembly.
Conclusion
The internal keyword provides assembly-level access control, making members accessible within the same assembly while preventing external access. This is essential for creating clean APIs and maintaining proper encapsulation in multi-assembly applications.
