Hi everyone, today I'll be talking about C# Access modifiers with examples so sit back and let's dive in
First, create a console application and you can name it AccessModTutorial. After that, create a class and call it anything you want, I'll call mine ExampleClass.
Next in your example class create some variables and assign different access modifiers to them, like this
Alright, so in our program class, let's try to interact with some of them. First the variable with a public access modifier
We see that we have access to the PublicName property because it is marked as "public" and allows access from anywhere in your code.
Next, let's try to access the PrivateName property from our Program.cs
Okay, so we get an error, and that is simple because properties marked as private are only accessible within the class it has been defined in. We can do a workaround by creating a constructor and assigning what we want to that property and then creating a method to return the value of that property
And now if we try to access the GetPrivateName method from our program.cs, we have no issues
Next, let's try to access the ProtectedName property that has an access modifier protected
Again, we get an error that we cannot access this property because of its protection level and that is because properties marked as protected can only be accessed from within the same class or a derived class. Now, we can solve this issue the same way we solved the one for the PrivateName property but let's see how we can solve it using a derived class.
Okay, we have made some changes, in the same file as your Exampleclass, we created another class called ChildExampleClass and specified that it inherits from ExampleClass. Then, just like we did in the Privatename property, we created a constructor and assigned what we want to the ProtectedName property and then created a function to return that value. So now if we try to access the method from our program.cs, we should not have issues.
Great, let's keep going.
Next, we have our PrivateproctedName property that is marked as private protected, which just means that the property is only accessible within the class and all derived classes but within the same assembly or project. So, just like we did with the ProtectedName property, we can assign a value we want in the constructor and create a function to return that value
And then we will be able to access it from our program.cs
But what would happen if we tried to access that property from another project or assembly? Let's try and see. Create a new project by right-clicking your solution(which I have highlighted in the image below)
then scroll to add --> New project. And create a new console app. I have called mine SecondAcessModProject
Next, in your SecondAccessModProject, create a new class, I
have called mine ChildExampleClass2 and let it extend ExampleClass. You will get an error, so don't forget to add a project reference by right-clicking on the SecondAccessModProject and going to Add --> Project Reference
And then add the reference to your first project that the ExampleClass is in.
Great so, now let's try to access the ProtectedPrivateName property.
As we mentioned before, that property is in a different assembly, so it is not accessible from here.
Great, now head back over to your first project, and let's try to access our InternalName property.
Properties marked as internal are accessible from anywhere within its assembly so we have no issues here. But what if we tried to access it from our other project?
And, we see that we do not have access to that property from our SecondAccessModProject.
Alright, let's log all the properties to the console to be sure all the values were changed.
And checking the console...
And that's it, now you understand how Access Modifiers work in C#.
If you'd like to get access to this code you can get it here
Top comments (2)
Hi ๐๏ธ
Why don't you use a code instead of a photo?!
The code is well-supported here.
The readability of the photo is low and cannot be copied, etc.
Hello Hamid,
Thanks for your feedback.
I decided not to use code because there are some things I want the readers to see on my screen, to be able to navigate the IDE better(ie showing console output and how to add a reference project). But I see now that the pictures are not very clear. I will work on replacing them to make the illustrations clearer.