Tuesday, 11 June 2024

Access Specifiers

Access Specifiers

Scope of Class and its members


Access Specifiers : Access Specifiers are used to define the scope of the type , Scope is nothing but visibility or accessibility of members. that is who can access them and who cannot access them are defined by the Access Specifiers.


What is the use of Access specifiers ?

Access specifier control access of application members and data, using this feature can decide who can access who can not access, this is a security feature so we can provide security to data in implementation level.


In .net c# have 6 type of access specifiers.

1.     Private

2.     Public

3.     Protected

4.     Internal

5.     Protected Internal

6.     Private Protected (C# Version 7.2 onwards)




These access specifiers  can apply to  Class, Interface, Structs, Delegate, Enum, etc.

And its members (Variables, Properties, Constructors, and Methods).






Private :- When we declare a type member (variable, property, method, constructor, etc) as private, then we can access that member with in the class only. From outside the class, we cannot access them.

Note: By default, all members of a class are private if you don't specify an access modifier:


Characteristics of Private Members:

When a class is marked as private, it means that the class can only be accessed within the same containing class or struct.

Typically, you use a private class when you need to encapsulate some functionality that is only relevant within the context of the containing class.



 


Private Property:

  • A private property is accessible only within the class or struct in which it is declared.
  • It allows you to encapsulate the internal state of your class and provide controlled access to it.



Private Constructor:

  • A private constructor is used to prevent a class from being instantiated outside of its containing class.
  • This is often used in scenarios such as implementing a singleton pattern or creating a class with only static members.



 

 


Public: (Access Everywhere)

  • A public class can be accessed from any other class or assembly.
  • It's commonly used when you want the class to be widely accessible, for example, as a part of a library or API.

 

Public Members are accessible in All areas No Restriction

  • With the Class
  • Derived Class in Same Assembly
  • Non-Derived Class in Same Assembly
  • Derived Class in Other Assemblies
  • Non-Derived Class in Other Assemblies



Protected

Protected members are accessible in same class and its derived/child/sub class in same assembly and other assembly when use inheritance.

 

1.     With the Class: YES

2.     Derived Class in Same Assembly: YES

3.     Non-Derived Class in Same Assembly: NO

4.     Derived Class in Other Assemblies: YES

5.     Non-Derived Class in Other Assemblies: NO



Internal :

Internal members are accessible anywhere within same assembly, outside assembly not accessible.

 

1.     With the Class: YES

2.     Derived Class in Same Assembly: YES

3.     Non-Derived Class in Same Assembly: YES

4.     Derived Class in Other Assemblies: NO

5.     Non-Derived Class in Other Assemblies: NO



Protected Internal Access Specifier or Access Modifier in C#:


No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.