DEV Community

Olabamiji Oyetubo
Olabamiji Oyetubo

Posted on

Understanding Access Modifiers in C#

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.

Create console app

Next in your example class create some variables and assign different access modifiers to them, like this

Create variables

Alright, so in our program class, let's try to interact with some of them. First the variable with a public access modifier

public 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

private modifier

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

Fix Private modifier error

And now if we try to access the GetPrivateName method from our program.cs, we have no issues

GetPrivatename method

Next, let's try to access the ProtectedName property that has an access modifier protected

Protected Modifier
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.

GetProtectedName

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.

Access Protected from Program.cs

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

Private Protected name

And then we will be able to access it from our program.cs

Access Private Protected name

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)

Add project
then scroll to add --> New project. And create a new console app. I have called mine SecondAcessModProject

Add Project

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

Add project reference

And then add the reference to your first project that the ExampleClass is in.

Add reference to first project

Great so, now let's try to access the ProtectedPrivateName property.

Access ProtectedPrivate from second project

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.

Internal Modifier

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?

Access Internal from another 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.

Log properties to the console

And checking the console...

Console Information

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)

Collapse
 
hamidmolareza profile image
Hamid Molareza

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.

Collapse
 
bigboybamo profile image
Olabamiji Oyetubo

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.