C# Access Specifiers
C# is an object oriented programming language in which there are different types of types like class struct, interface, enum, string, array, collection etc. These different types have accessibility or visibility that determines whether a type can be used within the project or outside the project. Within any type there are different types of members like property, method, constructor, indicator, field, etc. Within a class type there are different class members for which visibility or accessibility is also determined.
To determine accessibility, there are many modifiers in the C# such as public, private, protected, internal, protected internal, with the help of which the accessibility of a type or its members is determined.
There are only two Access Modifiers, internal and public, for a class.
Class visibility refers to whether a class is visible in its own project or other projects outside the project. The visibility of the class is decided by the modifier of the class. There are two modifiers possible for any C# class, first internal and second public. By default, C# classes are internal.
Remember that C# class has one of the following accessibility possible: Internal or Public.
Internal class means that the class will be visible throughout the project in which it is defined but the class will be invisible in any project other than its own.
Public class means that the class will visible not only in its own project, but will also be visible in any project other than its own project. That is, this class can be consumed by any project.
By default, the accessibility of the class is internal. It means that when modifier is not used with class then it will be internal class. Apart from this, other types of accessibility such as private, protected is not possible for any class.
The visibility and usability of the class are closely related. If a class is accessible by another class in a project, then that class can be used.
Internal class can be consumed by any other class within the project but one thing to note is that if the constructor of that class is private then that class cannot be consumed by other class. The reason for this is that if the constructor of the class is a private constructor, then in such a case no object of that class can be created by the other class. In such a situation, even though the class is internal, it cannot be consumed by another class.
Accessibility of class member functions or methods:-
There are five types of accessibility that can be given to a member function or method of a class under the C-sharp language, they are as follows
- public
- private
- protected
- internal
- protected internal
Public Method If the accessibility of a member function or method of a class is public, then that method can be accessed by any other class. The second class can be located within any project.
Private Method If the accessibility of a member function or method of a class is private then that method can be accessed only by the class within which this method is defined. In other words, the same class which has the method can consume that method. By default the method is private under C-sharp. In other words, the method is private if the access modifier is not provided.
Protected Method If the accessibility of a member function or method of a class is protected, then that method can be accessed not only by the class within which this method is defined, but also by all the child classes of that class. Method can be consumed. In other words, only the class that has the method and its child classes can consume that method. Due to the property of inheritance, the child class can access all the methods of its parent class which are not private.
Internal Method If the accessibility of a member function or method of a class is internal, then that method can be accessed by any other class that comes under the project or dll of the current class of the method. The second class can be located within the current project itself. Internal literally means internal. In other words, if a method is internal, it can only be used within the project within which the method is located. The internal method will be invisible in a project outside the internal method's project.
Protected internal method If the accessibility of a member function or method of a class is protected internal, then the features of the internal or protected method come together in that method. Within the internal project it acts as an internal method whereas within the external project it behaves as a protected method. So it is accessible by any class in the method's internal project. Apart from this, this method outside the project acts as a protected method and can be accessed by the class and child class of the method within the external project.
Edited on 5th July 2023
No comments:
Post a Comment