C# Part 51 - Internal, Protected Internal, Private Protected-Tutorial For Beginners

Опубликовано: 22 Март 2025
на канале: parvesh
15
1

1. Public
Accessibility: No restrictions.
Usage: The member is accessible from any other code in the same assembly or another assembly that references it.

2. Private
Accessibility: Limited to the containing type.
Usage: The member is accessible only within the body of the class or struct in which it is declared.

3. Protected
Accessibility: Limited to the containing type and derived types.
Usage: The member is accessible within its class and by derived class instances.

4. Internal
Accessibility: Limited to the current assembly.
Usage: The member is accessible within the same assembly, but not from another assembly.

5. Protected Internal
Accessibility: Combination of protected and internal.
Usage: The member is accessible within the same assembly or from derived types in other assemblies.

6. Private Protected
Accessibility: Limited to the containing class or types derived from the containing class within the same assembly.
Usage: The member is accessible within its containing class and by derived classes, but only within the same assembly.

Summary of Access Modifiers:

| Modifier | Same Class | Derived Class (same assembly) | Derived Class (different assembly) | Same Assembly | Different Assembly |
|---------------------|------------|-------------------------------|-------------------------------------|---------------|--------------------|
| Public | Yes | Yes | Yes | Yes | Yes |
| Private | Yes | No | No | No | No |
| Protected | Yes | Yes | Yes | No | No |
| Internal | Yes | Yes | No | Yes | No |
| Protected Internal | Yes | Yes | Yes | Yes | No |
| Private Protected | Yes | Yes | No | No | No |

These access modifiers allow fine-grained control over the visibility and accessibility of the members of a class, helping to enforce encapsulation and improve the maintainability of the code.