Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Access Modifiers in C#
3.
public Access Modifier in C#
4.
protected Access Modifier in C#
5.
internal Access Modifier in C#
6.
protected internal Access Modifier in C#
7.
private Access Modifier in C#
8.
private protected Access Modifier in C#
9.
Class, record, and struct accessibility
10.
Accessibility Table
11.
FAQs
12.
Key Takeaways
Last Updated: Mar 27, 2024

Access Modifiers in C#

Author Hari Sapna Nair
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Access modifiers are the keywords used in object-oriented languages to set the accessibility of methods, classes, and other members. It is a specific part of programming language syntax used to facilitate the encapsulation of components. They are also called access specifiers and they play an important role in any programming language.


We will now learn about access modifiers in C# and the various categories of access modifiers in C# like public, protected, internal, private, protected internal, and private protected.

Access Modifiers in C#

Access modifiers in C# are the keywords used to specify the accessibility or scope of variables and functions in the C#. Access modifiers in C# help us control the visibility of class members. It helps us achieve encapsulation and hide sensitive data is hidden from users. 

 

There are mainly four types of access modifiers in C#: public, protected, internal, and private. These access modifiers can be chosen to protect our data.

 

The access modifiers are as follows:-

Access Modifiers

Description

public It allows a part of the program in the same assembly or another assembly to access the type and its members.
private It restricts other program parts from accessing the type and its members. 
internal It allows other program codes in the same assembly to access the type or its members. Internal members or types are accessible only within files in the same assembly.
protected It allows codes in the same class or a derived class to access the type or its members.

 

There are also two other combinations of access modifiers in C#. They are:- 


Let's learn about each one of them in detail.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

public Access Modifier in C#

The public access modifier makes the data accessible publicly. It does not restrict the data to the declared block. This means that another method or assembly containing the class reference can access these types or members. It is the most permissive access level as there are no restrictions on accessing public members.

 

Syntax

public TypeName

 

Let's look at an example to better understand public access modifiers in C#.

 

Example

using System; 

class PublicClass  {  
   public string name = "Sapna";  
}  

class Program {
  public static void Main(string[] args) {
    PublicClass obj = new PublicClass ();  
    // Accessing a public variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Output

Hello Sapna

protected Access Modifier in C#

The protected access modifier limits access to the class that contains this class's member and derived types. It means that a class that is the subclass or the child class of the containing class can access the protected members.

 

Syntax

protected TypeName

 

Let's look at an example to better understand public access modifiers in C#.

 

Example

using System; 

class ProtectedClass  {  
   protected string name = "Sapna";  
}  

class Program {
  public static void Main(string[] args) {
    ProtectedClass obj = new ProtectedClass();  
    // Accessing a protected variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Error

An error occurs here as the protected string is not accessible. This can be solved by inheritance.

 

Modified Code

using System; 

class ProtectedClass  {  
   protected string name = "Sapna";  
}  

class Program: ProtectedClass {
  public static void Main(string[] args) {
    Program obj = new Program();  
    // Accessing a protected variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Output

Hello Sapna

 

In this case, the protected members are accessed within the child class by inheritance.

internal Access Modifier in C#

The internal access modifier limits access only within files in the same assembly. Any class or type declared as internal is accessible anywhere inside the same namespace. It is the default access modifier in C#.

 

Syntax

internal TypeName

 

Example

using System; 

class InternalClass  {  
   internal string name = "Sapna";  
}  

class Program {
  public static void Main(string[] args) {
    InternalClass obj = new InternalClass();  
    // Accessing an internal variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Output

Hello Sapna

protected internal Access Modifier in C#

In a protected internal access modifier, the access is limited to the current assembly or the derived types of the containing class. The access is granted to any class derived from the containing class within or outside the current assembly.

 

Syntax

protected internal TypeName

 

Example

class ProtectedInternalClass  {  
   protected internal string name = "Sapna";  
}  

class Program {
  public static void Main(string[] args) {
    ProtectedInternalClass obj = new ProtectedInternalClass();  
    // Accessing a protected internal variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Output

Hello Sapna

private Access Modifier in C#

The private access modifier limits access only to the containing class. Access to the members is not granted to any other class inside the current, or another assembly. It is the least permissive access level as private members are accessible only within the body of the struct or the class in which they are declared.

 

Syntax

private TypeName

 

Example

using System; 

class PrivateClass  {  
   private string name = "Sapna";  
}  

class Program {
  public static void Main(string[] args) {
    PrivateClass obj = new PrivateClass();  
    // Accessing a private variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Error

An error occurs here as the private string is not accessible outside the class in which it is declared. 

private protected Access Modifier in C#

In the private protected access modifier, access is granted to the containing class and its derived types present in the current assembly. 
 

Syntax

private protected TypeName

 

Example

using System; 

class PrivateProtectedClass  {  
  private protected string name = "Sapna";  
}  

class Program: PrivateProtectedClass {
  public static void Main(string[] args) {
    Program obj = new Program();  
    // Accessing a private protected variable  
    Console.WriteLine("Hello " + obj.name);  
  }
}

 

Output

Hello Sapna

 

Note: The private protected access modifier is valid in the C# 7.2 version and later.

Also see,  Ienumerable vs Iqueryable

Class, record, and struct accessibility

Classes, structs, and records declared directly within a namespace can be public or internal. The internal access modifier is the default if no other access modifier is specified.
 

Struct members, including nested structs and classes, can be declared public, private, or internal. Class members, including nested structs and classes, can be private,public, protected, internal, protected internal, or private protected. Struct and class members, including nested structs and classes, have private access by default. The private nested types aren't accessible from outside the containing type.
 

Derived records and classes can't have greater accessibility than their base types. We can't declare a public class B that derives from an internal class A. If allowed, it would make A public because all protected or internal members of A are accessible from the derived class.

Accessibility Table

The accessibility table of the above-discussed access modifiers are given below:

Caller's location

public

protected

internal

protected internal

private

private

protected

Within the class

✔️

✔️

✔️

✔️

✔️

✔️

Derived class (same assembly)

✔️

✔️

✔️

✔️

✔️

Non-derived class (same assembly)

✔️

✔️

✔️

Derived class (different assembly)

✔️

✔️

✔️

Non-derived class (different assembly)

✔️

FAQs

  1. Why do namespaces not allow the access modifier?
    Namespaces don't allow access modifiers as they have no access restrictions.
     
  2. What happens when there is no access modifier is specified for a member declaration?
    When we don't specify any access modifier for a member declaration, the default accessibility is used based on the context.
     
  3. What is the access modifier used in the enumeration members?
    The access modifier is always public for the enumeration members, and no other access modifiers can be applied to it.
     
  4. Why are access modifiers important?
    Access modifiers restrict unwanted data manipulation by external classes or programs.
     
  5. What is the use of "InternalsVisibleToAttribute" in C#?
    The "InternalsVisibleToAttribute" enables specific other assemblies to access the internal types.
     
  6. What is the default access modifier for the class members?
    By default, all class members are private if we don't specify an access modifier.

    Example:
class Student {
  string name;  // private
  int age;   // private
}

Key Takeaways

In this article, we have extensively discussed the access modifiers in C# and the various categories of access modifiers in C# like public, protected, internal, private, protected internal, and private protected in detail with the help of examples.

Related links:

We hope that this blog has helped you enhance your knowledge regarding the access modifiers in C# and if you would like to learn more, check out our articles on Loops in C#. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass