C #: from medium to advance level [Access modifier and Properties (Part I)]

Matteo Lo Piccolo
4 min readAug 17, 2021

Finally, here we are to talk about access modifier and properties. We start with access modifier.
They are : public, private, protected, internal and protected internal.

The reason why we use access modifier to make our code safe.
Let’s look at this example

I have Enemy Class, and create an instance of it.
Now, I have two fields, Name and Life, but I change them from public to private

If I go back in my Main method and try to compile

I don’t see nothing, because fields are private and I can’t access to them.

I made this little test only to explain three fundamental topics about OOP (Object Oriented Program) :

Encapsulation, Inheritance and Polymorphism.

We start with Encapsulation.
What is Encapsulation?

Encapsulation means in any Class we create, we hide the information from outside.
Rememeber? To make field public is really wrong and we can make some mistake, because if the fields are accessible from outside, we can change its the VALUE.
So, how can we do?
We can use private fields, or we use a “getter / setter” public properties.

It seems really hard, but after we understand the concept, everything becoming clear. Let’s look at this example

Ok, I have a private string Name.
So for now, this field is inaccessible from outside of this Class.

Now, one thing we can do is create a public methods that return the value of that field.

Here we simply said :
“We have a Name, but if call this method from outside, and we pass a string (not null and not empty) the value we pass is copied on Name”.

This is the “getter” part, and this is why the properties is incredible useful : we can assign a LOGIC to them.
In “field” we can’t do that, we simply assign a value.

The “setter” part is more easy, and it’s like this

This method is a string method, and return to us the VALUE of Name.

A precision here : because Name is private, a good practice is writing it like this

Make another example to understand better

Ok, we have Enemy Class with one field.
_damage is private, so we need two method like before to access to it

Now in Main method, I create an instance of enemy and I read the value of damage, then I change it, and I read it again

If we run the application

First value is 13, after I change it is 15.

Now, I know this seems incredibly stupid, because if we use access modifier public, we can make exactly same thing. So why we have to do something so “complicated” to obtain same result?

The first reason is the concept of Encapsulation, and is is one of the pillar of OOP.
The fields must be hidden from the outside and revealed only when we need them, even if sometimes seems nothing really change.
But in OOP prospective, this is the right pathway to follow.

Second, this is just an example, normally we don’t use two methods for define fields, but, in fact, we use properties!

This example only need to us to understand what happen “behind the scene”.
In the next article, finally, we look at Properties.

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!