I hope that you have already studied previous lectures i.e. C# Methods, C# Structs, C# Classes etc. as these access modifiers are used with them. So, let’s get started with Introduction to Access Modifiers in C#:
Introduction to Access Modifiers in C#
- Access Modifiers in C# are used to apply restrictions on the accessibility of C# Objects within or outside the project, and are defined in the declaration.
- There are 5 Access Modifiers available in C#, whcih are:
- Protected Internal
- We will have a look at all of them, in detail but before going any further, we need to understand the difference between Types & Type Members.
- All those C# objects, which can contain members in them as called Types i.e. C# Classes, Structs, Interface, Enums etc.
- While the members of these types are called Type Members i.e. C# Fields, C# Properties and C# Methods.
- All the C# Types can only be either Public or Internal i.e. we can’t make a class private. ( We will discuss it in detail shortly )
1. Private – Access Modifier in C#
- Private Access Modifier restricts the C# Type Members to be used in the containing class only. ( Containing Class means, the class in which they are created )
- We can’t use Private Members in external classes, as shown in the figure on right side.
- I have created a Private variable in a new class and now when I am trying to access this variable in Main function, I am unable to do that.
- In the IntelliSense, there’s no property/option to use this private variable.
- Although, we can use this variable Number1 in HelloWorld class quite easily.
2. Public – Access Modifier in C#
- Public Access Modifier removes all restrictions from C# Objects and these objects become available in any assembly or project.
- In order to use Public Objects, we need to follow the proper way i.e. we need to instantiate in order to get the Public Property.
- In the figure, you can see I have created a public variable Number1 in a new class and then I have assigned a value to it in my Main Method.
3. Protected – Access Modifier in C#
- Protected Access Modifier restricts the C# Type Members to use only in containing classes or inherited classes.
- We can’t use Protected Members in any independent Class or Struct, it has to be inherited from the Containing Class.
- Here’s an example of Protected Access Modifier in action, and you can see that I can assign value to Protected variable Number1 in the inherited class HelloWorld2.
- But the independent class Program can’t access this Number1 variable.
4. Internal – Access Modifier in C#
- Internal Access Modifier restricts the C# Objects to be used in the current assembly only.
- In complex projects, we need to add multiple assemblies i.e. different add-ons and plugins etc.
- So, Internal variables can be used in the current assembly / project only, we can’t access it in external assemblies.
- We will look at them in detail, in our coming lectures.
5. Protected Internal – Access Modifier in C#
- Protected Internal Modifier allows the C# Type Members to be accessed in the containing assembly as well as in those classes ( of external assemblies ), which are inherited from the containing class.
- When we will be done with these basic concepts in C#, then we will design few complex projects and then you will get better understanding of these access Modifiers.
So, that was all about Access Modifiers. In the coming lecture, we will have a look at Enumerations in C#. Till then take care & have fun !!! 🙂