Access Modifiers help us protect the data.
- By default, Access Modifier for a class is internal.
- By default, Access Modifier for Data members is private.
- By default, Access Modifier for method is private.
C# offers 5 types of Access Modifiers, as shown below:
- Private
- Public
- Protected
- Internal
- Protected Internal
Private: Data members and methods are accessed in the same class.
Public: Data members and methods are accessed in the same assembly or another assembly which reference them.
Protected: Data members and methods are accessed in the same class or in a derived class.
Internal: Data members and methods are accessed in the same assembly.
Protected Internal: Data members and methods are accessed in the same assembly or any derived class in another assembly.
Private and Public Access modifiers Example Code
In the below code, orderID is Private. So, orderID cannot be accessed in AccessModifiersExample class. But, it can be accessible in myOrders class only.
In the below code, items and description are Public. So, items and description are accessible in AccessModifiersExample class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication
- {
- class AccessModifiersExample
- {
- static void Main(string[] args)
- {
- myOrders objMyOrders = new myOrders();
-
- objMyOrders.items = "4";
- objMyOrders.description = "Shoes";
-
- }
-
- }
- class myOrders
- {
- private int orderID { get; set; }
- public string items { get; set; }
- public string description { get; set; }
-
- public void getOrderDetails(int pOrderID)
- {
- orderID = pOrderID;
- }
- }
- }
Protected Access modifiers Example Code
In the following code, price is protected. When we inherited myOrders class to AccessModifiersExample, then only it can access the price property.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication
- {
- class AccessModifiersExample: myOrders
- {
- static void Main(string[] args)
- {
- AccessModifiersExample objAccessModifiers = new AccessModifiersExample();
-
- objAccessModifiers.items = "4";
- objAccessModifiers.description = "Shoes";
-
- objAccessModifiers.price = 200.0;
- }
-
- }
- class myOrders
- {
- private int orderID { get; set; }
- public string items { get; set; }
- public string description { get; set; }
-
- protected double price { get; set; }
-
- public void getOrderDetails(int pOrderID)
- {
- orderID = pOrderID;
- }
- }
- }
Internal Access modifiers Example Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication
- {
- class AccessModifiersExample
- {
- static void Main(string[] args)
- {
- myOrders objOrders = new myOrders();
-
- objOrders.items = "4";
- objOrders.description = "Shoes";
-
- objOrders.isShipped = true;
- }
-
- }
- class myOrders
- {
- private int orderID { get; set; }
- public string items { get; set; }
- public string description { get; set; }
-
- protected double price { get; set; }
-
- internal bool isShipped { get; set; }
-
- public void getOrderDetails(int pOrderID)
- {
- orderID = pOrderID;
- }
- }
- }