Understanding Access Modifiers in C#
Tags: csharp, beginners, webdev, programming
Published: 2023-12-09 14:00:58

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u55m1n6sjvcw0ddny81y.png)

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

![Create variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9kr0ai4d4tbzv7fjo3g.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1u8q1x4f6wufnnqhe2n.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zhticorjn24enldutms.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wddr78q5rcgz71brpv3s.png)

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

![GetPrivatename method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm22yqg02go8j57oq8zp.png)

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

![Protected Modifier](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/badq1oruw672q7h2dlxe.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjc4yhty9bcju7zzc476.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rhhmikpffao10gihjwo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jiwwfymynsobju6temkl.png)

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

![Access Private Protected name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zob4wblbvjxdckej3jbk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75fxwh0n02a2au31049a.png)
then scroll to add --> New project. And create a new console app. I have called mine **SecondAcessModProject**

![Add Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fauwfgxyu84bb70ho5ri.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmdjuuiqwzvy82t261bx.png)

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

![Add reference to first project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6051sodju5tlmrz5d8s.png)

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

![Access ProtectedPrivate from second project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edml5ynkjqw4oomdry9r.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st1isurtabe2s313vjdh.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjbrsu9kn6w636hmwwr7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1unj3nb9oaieobgdija.png)

And checking the console...

![Console Information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ldk0ty1fpg6r23d0ua9.png)

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](https://github.com/bigboybamo/AccessModifiers)