C# : Access modifiers : Part 19
- Public : Accessible anywhere no restriction.
- Private : Accessible only in the containing type.
- Protected : Within containing types and the types derived from containing types.
- Internal : Accessible anywhere in the containing assembly, not accessible in other assembly even after adding reference.
- Protected Internal : In containing assembly and accessible within a derived class from any other assembly.
Containing type mostly refers to class
Access Modifier : Public
- As you can see in the image(1) below that Marks property is not accessible when instance of student class is created, all other public properties are accessible
Access Modifier : Private
- As seen in image(1) above the property Mark is not accessible because it is private, so it is not accessible outside the containing type, i.e. class
- Where as private property are easily accessible within the containing type, i.e. within the class
- In figure below image(2) Marks field is accessible as it is in the same class.
Access Modifier : Protected
- The properties with protected access modifiers are accessible in same class/containing types and from the type derived from the containing types.
- As seen in image(3) below only DemoFunction method is accessible as it is public image(3)
- In image(4) below the instance of Student class is created, where only public property Surname is accessible image(4)
- In image(5) the StudentDetils class inherited from Student class, so now if the instance of StudentDetails is created the protected property Class will be accessible image(5)
- The property / method / class declared as internal will be only accessible from anywhere in the same assembly / (DLL) / same project.
- If the reference of the project is given to another project and if tried to access the property which is marked as internal than it willl throw compile time error
- The properties marked as internal protected are accessible anywhere in the same assembly / (DLL) / same project.
- They are also accessible from the derived class from any other assembly.
Access Modifier : Internal
Access Modifier : Internal Protected
No comments