Access Modifiers or Access Specifiers in C #



In this article we will learn C# Access Modifiers (Access Specifiers) and what their purpose is.
Firstly we learn what is an Access Modifier? Why use access modifiers?
Access Modifiers
Access Specifiers or Access Modifiers is used to describe the visibility of a method or class property. Access modifiers are delineated as the scope of intelligibility of an Object and its members. We can ascendancy the scope of the member object of a class using access modifiers. We can provide security for our application using access modifiers.  When we use class modifiers then it restricts access so that other programs cannot see the properties. Access Modifiers keyword used to specify the define accessibility of a member or a type.
For example, when we declare a public method in the class is accessible to everyone without any diminution, while an internal class may be accessible to the body only.

Why use Access Modifiers?
Access modifiers are an essential part of object-oriented programming. Access Modifiers allow you to define who can access or who can’t access to exact features.
Following are the 5 types of Access Modifiers.
1.        Public
2.        Private
3.        Protected
4.        Internal
5.        Protected Internal
In that four is the main type (Public, Private, Protected, Internal) and one is combinations type (Protected Internal).







1.        Public
The public method is accessible to everyone or anywhere without any restrictions or there are no restrictions on accessing public members. When public method is attached to either a property, it means that those members can be accessed from any external program code in the same assembly or another assembly. The range of the accessibility is inside a class as well as outside a class.

For example:
class EmployeeDetails
{
               public int eID = 101;
}

Class Employee
{
               static void Main(string[] args)
               {
                             EmployeeDetails emp = new EmployeeDetails();
                             Console.WriteLine(emp.eID);
               }
}











2.        Private
When the private method is attached to either property, it means that those members cannot be accessed from any external program code in the same assembly or another assembly. Private access modifiers access is limited to the same assembly or another assembles. The range of the accessibility is limited only inside the classes in which they are declared. The Private method members cannot be accessed outside the class or these method members are accessible only within the body of the class in which they are declared.

For example

class EmployeeDetails
{
               private int eID = 101;
}

Class Employee
{
               static void Main(string[] args)
               {
                             EmployeeDetails emp = new EmployeeDetails();
                             Console.WriteLine(emp.eID);
               }
}
Now above example we get error, we can’t access the eID variable because it that the private access modifier and it’s only accessible in the EmployeeDetails class.















3.        Protected
In protected method access is limited to the class definition and any class that inherits from the class. When the protected method is attached to either property, it means that those members can be accessed only by classes inherited from the current class. We will discuss that thing in details in the Inheritance. A protected method member of a base class is accessible in a derived class only if the access takes place through the derived class type.


class EmployeeDetails
{
               protected int eID = 101;
}

class CompanyDetails: EmployeeDetails // this is inheritance. CompanyDetails derives from the EmployeeDetails class
{
               void Print()
               {
                             Console.WriteLine(eID); // we can access it in this class
               }
}

Class Employee
{
               static void Main(string[] args)
               {
                             EmployeeDetails emp = new EmployeeDetails();
                             Console.WriteLine(emp.eID); // here we get error. The number varibale is inaccessible dut to its protection level.
               }
}






4.        Internal
The internal access modifier is limited to the current assembly. When the internal method is attached to either property, it means that those members can be accessed within the program that contains its declarations and also accesses within the same assembly level but not from another assembly. We can declare a class as internal or its member as internal using this method.



public class EmployeeDetails
{
               internal int eID = 101;
}

Class Employee
{
               void Print()
               {
                             EmployeeDetails emp = new EmployeeDetails();
                             Console.WriteLine(emp.eID); // That is okay. Anywhere in this project we can access the eID variable.
               }
}
C











5.        Protected Internal

Protected Internal access is limited to the current project or current assembly from the containing class. Protected Internal is the same levels of both the protected and internal methods. Protected Internal can access anywhere in the same project and in the same class also the classes inherited from the same class. The Protected Internal means protected or internal not a protected and internal.

public class EmployeeDetails
{
               protected internal int eID = 101;
}

Class Employee
{
               void Print()
               {
                             EmployeeDetails emp = new EmployeeDetails();
                             Console.WriteLine(emp.eID); // That is okay. Anywhere in this project we can access the eID variable.
               }
}




Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/



Comments

Popular posts from this blog

ALTER statement in MySQL

Order By Clause in MySQL

MySQL Constraint – UNIQUE