
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are different types of access modifiers available in C#?
Access modifiers are used to specify the scope of accessibility of a member of a class or type of the class itself. There are six different types of access modifiers.
Public
Private
Protected
Internal
Protected Internal
Private Protected
Public Access Modifier
Objects that implement public access modifiers are accessible from everywhere in a project without any restrictions.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Person Name is accessible as it is public } } public class Person{ public string Name = "Mark"; } }
Private Access Modifier
Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Since Name is private it is not accessible in Program class. // Error: Person.Name is inaccessible due to its protection level. } } public class Person{ private string Name = "Mark"; } }
Protected Access Modifier
The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ Employee employee = new Employee(); employee.Print(); //Output: Mark Person person = new Person(); Console.WriteLine(person.Name); // Error: Person.Name is inaccessible due to its protection level. } } public class Person{ protected string Name = "Mark"; } public class Employee : Person{ public void Print(){ Console.WriteLine(Name); } } }
Internal Access Modifier
For Internal keyword, the access is limited exclusively to classes defined within the current project assembly.
Example
Project 1 −
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Output: Mark } } public class Person{ internal string Name = "Mark"; } }
Output
Mark
Project 2 −
using MyApplication; using System; namespace Project2{ public class Project2Class{ public void Print(){ Person person = new Person(); Console.WriteLine(person.Name); // Error: Person.Name is inaccessible due to its protection level. } } }
Protected Internal Access Modifier −
The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies.
Example
Project 1 −
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Output: Mark } } public class Person{ protected internal string Name = "Mark"; } }
Output
Mark
Project 2 −
using MyApplication; using System; namespace Project2{ public class Project2Class : Person{ public void Print(){ Console.WriteLine(Name); //Output: Mark } } }
Private Protected Access Modifier
The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error. This modifier is valid in C# version 7.2 and later.
- Related Articles
- Types of access modifiers in Java?
- What are access modifiers and non-access modifiers in Java?
- What are the different types of DOM available to access and modify content in JavaScript?
- What are the different types of LED available?
- What are access modifiers in C++?
- What are different types of multiple access protocols?
- What are the differences between access modifiers and non-access modifiers in Java?
- What are the different types of wait available in Selenium?
- What are different types of RAM (Random Access Memory) in computer architecture?
- What are the different types of point available in geom_point of ggplot2 package in R?
- What are final, abstract, synchronized non-access modifiers in Java?
- Access Modifiers in C++
- Access Modifiers in Java
- Access Modifiers in C#
- What are private, public, default and protected access Java modifiers?
