Access Modifiers In C#

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:

  1. Private
  2. Public
  3. Protected
  4. Internal
  5. 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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication  
  8. {  
  9.     class AccessModifiersExample  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             myOrders objMyOrders = new myOrders();  
  14.   
  15.             objMyOrders.items = "4";  
  16.             objMyOrders.description = "Shoes";  
  17.   
  18.         }  
  19.   
  20.     }  
  21.     class myOrders  
  22.     {  
  23.         private int orderID { getset; }  
  24.         public string items { getset; }  
  25.         public string description { getset; }  
  26.   
  27.         public void getOrderDetails(int pOrderID)  
  28.         {  
  29.             orderID = pOrderID;  
  30.         }  
  31.     }  
  32. }  
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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication  
  8. {  
  9.     class AccessModifiersExample: myOrders  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             AccessModifiersExample objAccessModifiers = new AccessModifiersExample();  
  14.   
  15.             objAccessModifiers.items = "4";  
  16.             objAccessModifiers.description = "Shoes";  
  17.   
  18.             objAccessModifiers.price = 200.0;  
  19.         }  
  20.   
  21.     }  
  22.     class myOrders  
  23.     {  
  24.         private int orderID { getset; }  
  25.         public string items { getset; }  
  26.         public string description { getset; }  
  27.   
  28.         protected double price { getset; }  
  29.   
  30.         public void getOrderDetails(int pOrderID)  
  31.         {  
  32.             orderID = pOrderID;  
  33.         }  
  34.     }  
  35. }  
Internal Access modifiers Example Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication  
  8. {  
  9.     class AccessModifiersExample  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             myOrders objOrders = new myOrders();  
  14.   
  15.             objOrders.items = "4";  
  16.             objOrders.description = "Shoes";  
  17.   
  18.             objOrders.isShipped = true;  
  19.         }   
  20.   
  21.     }  
  22.     class myOrders  
  23.     {  
  24.         private int orderID { getset; }  
  25.         public string items { getset; }  
  26.         public string description { getset; }  
  27.   
  28.         protected double price { getset; }  
  29.   
  30.         internal bool isShipped { getset; }  
  31.   
  32.         public void getOrderDetails(int pOrderID)  
  33.         {  
  34.             orderID = pOrderID;  
  35.         }  
  36.     }  
  37. }  
Ebook Download
View all
Learn
View all